2012-05-22 41 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; 
} 
相關問題