2

我刚刚开始学习 Qt,我正在尝试使用这个 QUiLoader 创建一个简单的小部件。但我收到此错误:“设计器:读取第 1 行第 0 列的 UI 文件时发生错误:文档过早结束。”

#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

1 回答 1

4

您需要将 .ui 文件添加到项目的资源中。资源是在您的应用程序中“编译”的文件,然后可以通过以":/".

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

然后在您的 .pro 文件中出现一个部分:

RESOURCES += \
    resources.qrc

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

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


如果您是 Qt 新手,您可能不知道您还可以让 Qt在构建过程中为您的 ui 文件创建一个类文件。当您在该部分的 pro 文件中列出您的 ui 文件时,这已经完成FORMS +=

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

所以有两个类: * 为您的 ui 文件自动生成的类,称为Ui::Aks(在命名空间 Ui 中),可在ui_aks.hbuild 文件夹中的文件中找到。* 你自己的包装类;acutal 小部件类,它使用 ui 类。

如果您想手动创建第二个类,您可以编写(当您添加设计器表单类而不是仅设计器表单时,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;
}
于 2012-05-22T13:52:53.043 回答