在Qt中处理多个ui文件 [英] Handling multiple ui files in Qt
问题描述
我是新的Qt框架,我试图加载另一个UI文件时SubmitClicked。文件名为Form.ui
I am new to the Qt framework and am trying to load another UI file when SubmitClicked. The file's name is Form.ui
//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: SubmitClicked()
{
Form* f= new Form(this);
f->show();
f->raise();
f->activateWindow();
}
//Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
这没有成功!你能告诉我什么错了吗?
This didn't work out! Can you tell me what's wrong?
推荐答案
.ui文件就是存储GUI元素代码的地方。此代码在大多数情况下由QtDesigner生成。它类似于Visual Studio的.rc文件和向导生成GUI的函数和形式。这个文件将在编译时加载,这是默认的或在运行时通过 QUiLoader
。如果你想在运行时动态生成UI,后者是你需要查看的开始 QtUiTools
The .ui file is simply where the code for GUI elements gets stored. This code is generated by the QtDesigner in most cases. It's similar to Visual Studio's .rc file and wizard generated GUI in function and form. This file will either be loaded at compile time which is the default or at runtime via the QUiLoader
. If you want dynamically generated UI at runtime the latter is the option you need to look into starting with QtUiTools
在旁边的注释中,类 Form
在Qt中不存在,因此这是你做的类或打字错误。如果你只是想声明并显示一个窗口或对话框,然后从适当的基类派生并调用 show()
或适当的方法。
On a side note the class Form
does not exist in Qt so this is either a class you made or a typo. If you simply want to declare and show a window or dialog then derive from the appropriate base class and call show()
or the appropriate method.
例如这样的简单例子 MainWindow
是你自己的用户定义类派生自 QMainWindow
:
For example something simple like this where MainWindow
is your own user defined class derived from QMainWindow
:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(application);
QApplication app(argc, argv);
MainWindow mainWin;
mainWin.show();
return app.exec();
}
编辑:
Ah Form
是一个 QWidget
类。您是否缺少表单
类中的 Q_OBJECT
宏?你通常只调用 setupUi
一次为应用程序的主窗口加载您的资源,并且像用户定义的子类,它通常更容易定义gui对象类的编程。
Ah so Form
is a QWidget
class. Are you missing the Q_OBJECT
macro in your Form
class? You also generally only call setupUi
once for the main window of the application to load your resources and such where as user defined subclasses it's often easier to define gui objects for the class programmatically.
//Form.h
class Form : public QWidget
{
Q_OBJECT // this is needed for the MOC aka qmake
public:
Form(QWidget *parent);
virtual ~Form();
private:
QTextEdit m_text;
};
//Form.cpp
#include "form.h"
Form::Form(QWidget *parent) : QWidget(parent)
{
setCentralWidget(&m_text);
}
Form::~Form()
{
}
这听起来好像是你的类对象与你的ui命名空间文件混淆。
It sounds almost like you're confusing your class object with your ui namespace files.
这篇关于在Qt中处理多个ui文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!