2012-05-22 48 views
2

我刚开始学习Qt,我试图用这个QUiLoader创建一个简单的小部件。但是我得到这个错误:“Designer:在第1行第0列读取UI文件时发生错误:文档过早结束。”Qt - 使用QUiLoader加载一个没有父项的小部件

#include <QtGui/QApplication> 
#include <QtUiTools/QUiLoader> 
#include <QFile> 


int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    QUiLoader loader; 
    QFile file(":/aks.ui"); 
    file.open(QFile::ReadOnly); 

    QWidget *myWidget = loader.load(&file); 
    if(myWidget){ 
     myWidget->show(); 
    } 

    return a.exec(); 
} 

我构建了使用QtCreator 2.4.1的ui文件,我在Qt 4.7.4上。查看ui文件。

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>Form</class> 
<widget class="QWidget" name="Form"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>131</width> 
    <height>129</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>Form</string> 
    </property> 
    <layout class="QGridLayout" name="gridLayout"> 
    <item row="0" column="0"> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
     <widget class="QCheckBox" name="checkBox"> 
     <property name="text"> 
     <string>A</string> 
     </property> 
     </widget> 
    </item> 
    <item> 
     <widget class="QCheckBox" name="checkBox_2"> 
     <property name="text"> 
     <string>B</string> 
     </property> 
     </widget> 
    </item> 
    <item> 
     <widget class="QCheckBox" name="checkBox_3"> 
     <property name="text"> 
     <string>C</string> 
     </property> 
     </widget> 
    </item> 
    <item> 
     <widget class="QCheckBox" name="checkBox_4"> 
     <property name="text"> 
     <string>D</string> 
     </property> 
     </widget> 
    </item> 
    <item> 
     <widget class="QCheckBox" name="checkBox_5"> 
     <property name="text"> 
     <string>E</string> 
     </property> 
     </widget> 
    </item> 
    </layout> 
    </item> 
    <item row="0" column="1"> 
    <widget class="QPushButton" name="pushButton"> 
    <property name="text"> 
     <string>PushButton</string> 
    </property> 
    </widget> 
    </item> 
    </layout> 
</widget> 
<resources/> 
<connections/> 
</ui> 

我的项目文件:

#------------------------------------------------- 
# 
# Project created by QtCreator 2012-05-21T19:48:31 
# 
#------------------------------------------------- 

QT  += core gui 

TARGET = Example 
TEMPLATE = app 


SOURCES += main.cpp \ 
    sortdialog.cpp 

HEADERS += \ 
    sortdialog.h 

FORMS += \ 
    sortdialog.ui \ 
    aks.ui 

CONFIG += uitools 

回答

4

你需要你的.ui文件添加到您的项目资源。资源是可以在应用程序中“编译”的文件,然后通过从":/"开始的文件路径可用于Qt类。

为了向您的项目添加资源,您需要创建一个资源文件,列出要注册为资源的文件。这个文件将是另一个XML文件,可以由QtCreator创建和编辑。在项目管理器中,添加另一个文件并从对话框中选择类型Qt - > Qt资源文件。

在你的.pro文件,然后会出现一个部分:

RESOURCES += \ 
    resources.qrc 

在您需要添加一个前缀的资源文件;只需将其命名为/(或将其保留为空)。在此前缀中,您可以添加文件aks.ui,因此它将被命名为:/aks.ui

请注意,此类型的UI创建在运行时发生。这意味着它更加灵活(也许ui文件只在运行时创建),但速度稍慢(因为解析和更多运行时处理发生)。


如果你是新来的Qt,你可能不知道,你也可以让Qt的创建一个类文件在构建过程中您的UI文件。当您在FORMS +=部分的专业文件中列出您的ui文件时,这已经完成。

要使用自动构建的类,您还应该创建一个设计器表单类,这是您将自己的代码放入其中的另一个类。这个类将加载自动构建的类来设置你的UI。

所以有两类: *您UI文件自动生成的类,称为Ui::Aks(命名空间中的UI),在build文件夹中的文件ui_aks.h中发现的。 *您自己的包装类;使用ui类的acutal小部件类。

如果你想手动创建第二类,你可以写(QtCreator其实不正是这一步,当你添加的,而不是只有一个设计师形式的设计形式类):

aks.h:

#ifndef AKS_H 
#define AKS_H 

#include <QWidget> 

namespace Ui { 
    class Aks; // <-- this is the automatically created ui class which we need 
} 

class aks : public QWidget // <-- your own widget class using the designer UI 
{ 
    Q_OBJECT 

public: 
    explicit Aks(QWidget *parent = 0); 
    ~Aks(); 

private: 
    Ui::Aks *ui; // <-- instance of the automatically created ui class 
}; 

#endif // AKS_H 

aks。CPP:

#include "aks.h" 
#include "ui_aks.h" // <-- include the automatically generated ui class 

Aks::Aks(QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::Aks) 
{ 
    ui->setupUi(this); // <-- here, the widgets from the ui file get created 
} 

Aks::~Aks() 
{ 
    delete ui; 
} 
相关问题