English 中文(简体)
Qt - 使用 QUilooader 装入没有父控件的部件
原标题:Qt - Using QUiLoader to load a widget with no parent

我刚刚开始学习 Qt, 我正试图使用此 QUiLoader 创建一个简单的部件。 但我正在获取错误 : “ 设计师: 在第一行第 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
最佳回答

您需要将您的. ui 文件添加到您的工程资源中。 资源是那些在应用程序中“ 编译在” 的文件, 然后从 < code> 开始, 即 / " 开始, 通过文件路径提供给 Qt 类 。

要为您的工程添加资源, 您需要创建一个资源文件, 列出您想要注册的文件为资源。 此文件将是另一个 XML 文件, 可以由 QtCreator 创建和编辑。 在工程管理器中, 添加另一个文件, 从对话框中选择 Qt - & gt; Qt 资源文件类型 。

在您的. pro 文件中, 然后出现一个区域 :

RESOURCES += 
    resources.qrc

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

请注意, 这种类型的 UI 创建是在运行时间 < / 坚固 > 中进行 < 坚固 > 。 这意味着, 它更灵活( 可能是 ui 文件仅在运行时间创建), 但稍慢一点( 因为解析和一些更多运行时间处理发生 ) 。


如果您重新加入 Qt, 您可能不知道您也可以让 Qt 为您的 ui 文件创建类文件 < strong> 在构建进程 < / strong > 中 。 在 < code> forumMS \ / code > 部分的 pro 文件中列出您的 ui 文件时, 已经这样做了 。

要使用自动构建的类, 您也应该创建设计师表格类, 这是您将自己的代码放入其中的另一类。 此类会自动装入自动构建的类来设置您的 ui 。

So there are two classes: * The automatically generated class for your ui file, called Ui::Aks (in a namespace Ui), found in the file ui_aks.h in the build folder. * Your own wrapper class; the acutal widget class, which uses the ui class.

如果您想要手动创建第二类, 您可以写入( 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

<% 1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#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;
}
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签