Chinaunix首页 | 论坛 | 博客
  • 博客访问: 183747
  • 博文数量: 20
  • 博客积分: 1510
  • 博客等级: 上尉
  • 技术积分: 214
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-11 23:54
个人简介

一个异想天开的coder

文章分类

全部博文(20)

文章存档

2014年(5)

2013年(4)

2012年(2)

2011年(1)

2008年(1)

2007年(6)

2006年(1)

分类: C/C++

2013-05-05 18:47:14

写QT程序里运行时加载UI文件,代码如下:

点击(此处)折叠或打开

  1. #include "keyboard.h"
  2. #include <QtUiTools>
  3. #include <QWSInputMethod>
  4. #include <QVBoxLayout>
  5. #include <QFile>

  6. Keyboard::Keyboard(QWSInputMethod *im)
  7.     : QWidget(0, Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint)
  8. {
  9.     QUiLoader loader;

  10.     m_im = im;

  11.     QFile file(":/keyboard.ui");
  12.     file.open(QFile::ReadOnly);
  13.     QWidget *uiWidget = loader.load(&file, this);
  14.     file.close();

  15.     QVBoxLayout *layout = new QVBoxLayout;
  16.     layout->addWidget(uiWidget);
  17.     setLayout(layout);
  18. }
运行时产生以下错误:
"Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document."
经分析原来是因为QFile file(":/keyboard.ui")这语句打开的文件以:\开头的文件名是在QT资源文件中定义的文件,而我的QT项目中并没有资源文件。因此要么去掉":/", 要么定义QT资源文件qrc,并在.pro文件中加入此资源文件:
RESOURCES += \
    resources.qrc
阅读(7991) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

10533045712013-06-01 11:05:47

在看 C++ GUI  Qt4  编程  这本书的时候,书上给的范例 没有 file.open(QFile::ReadOnly); 这个步骤 ,会导致出现这样的错误
并且在使用QtCreator的时候,创建的 *.ui  文件由于不在  Debug  或 Release 目录下 ,直接 用   “***.ui”    引用文件的话
也会导致上述的错误,原因是找不到文件,手工将文件拷贝过去就OK 了 。