简体   繁体   English

如何使用button.clicked()事件在Qt中调用创建的.ui设计表单?

[英]How to call created .ui design form in Qt with button.clicked() event?

I'm very new in Qt and I would like to ask how to call the created .ui designer form to be connected with the PushButton in MainWindow. 我在Qt中很新,我想问一下如何调用创建的.ui设计器表单与MainWindow中的PushButton连接。

What I've done: 1. Created a new .ui design form (Forms.right click -> Add New -> Qt Designer Form) to the current project. 我做了什么:1。创建一个新的.ui设计表单(Forms.right click - > Add New - > Qt Designer Form)到当前项目。 2. Designed an about dialog in the .ui form created. 2.在.ui表单中创建了一个about对话框。 3. Make a slot PushButton.clicked() in the MainWindow. 3.在MainWindow中创建一个插槽PushButton.clicked()。

For all who have done programming in Qt, please help me solve this problem to connect the PushButton with the .ui form, so the PushButton can call/show the form created in the .ui. 对于所有在Qt中编程的人,请帮我解决这个问题,将PushButton与.ui表单连接起来,这样PushButton就可以调用/显示在.ui中创建的表单。

Thanks for your attention. 感谢您的关注。

Using .ui files is explained in Qt's documentation: http://doc.qt.io/archives/qt-4.7/designer-using-a-ui-file.html 在Qt的文档中解释了使用.ui文件: http//doc.qt.io/archives/qt-4.7/designer-using-a-ui-file.html

Summary: The easiest way to handle an .ui file is to run it through uic (the User Interface Compiler) at compile time. 简介:处理.ui文件的最简单方法是在编译时通过uic (用户界面编译器)运行它。 This is done automatically if the .ui file is included in your project (.pro) file. 如果.ui文件包含在项目(.pro)文件中,则会自动完成此操作。 ("Add New" probably has done this automatically in your case.) Then you only need to include the generated C++ header file in your source file. (“添加新”可能在您的情况下自动执行此操作。)然后,您只需要在源文件中包含生成的C ++头文件。 Its name should be something like "ui_nameoftheoriginaluifile.hpp". 它的名字应该是“ui_nameoftheoriginaluifile.hpp”。 Of course, after that you need to instantiate the form defined in the .hpp file. 当然,之后您需要实例化.hpp文件中定义的表单。

Edited to add: 编辑添加:

There are several issues with your code, starting from its readability. 从可读性开始,您的代码有几个问题。 I don't know if you have used an object-oriented language before, but a very basic rule in C++ is to begin class names with capital letters to make them easier to distinguish from objects and other variables. 我不知道你以前是否使用过面向对象的语言,但C ++中一个非常基本的规则是用大写字母开始类名,以便更容易区分对象和其他变量。 So the class names should be "About", "Parent", etc. 因此,班级名称应为“关于”,“父母”等。

The compilation error is caused by using "about" instead of the name of the class actually used in the "ui_about.hpp" file - "MainWindow". 编译错误是由使用“about”而不是“ui_about.hpp”文件中实际使用的类的名称 - “MainWindow”引起的。 (Which is determined by the name of the form you have used in the .ui file.) (这取决于您在.ui文件中使用的表单的名称。)

If you are using Qt Creator: hold down the Ctrl key and click on the name of the file "ui_about.h" in the include directive. 如果您正在使用Qt Creator:按住Ctrl键并单击include指令中文件“ui_about.h”的名称。 This will open it for inspection. 这将打开它进行检查。 Try to figure out how it works. 试着弄清楚它是如何工作的。

You also haven't defined the opDialog() function as a member of the "parent" class in "parent.cpp", which causes another compile-time error. 您还没有将opDialog()函数定义为“parent.cpp”中“父”类的成员,这会导致另一个编译时错误。

You also shouldn't be using a QMainWindow for an about dialog. 您也不应该使用QMainWindow进行关于对话框。 QMainWindow is supposed to be the main window of your application - there shouldn't be any more instances of it. QMainWindow应该是你的应用程序的主窗口 - 不应再有它的实例。

So, about.h: about,about.h:

namespace Ui {
    class MainWindow;
}

class about : public QMainWindow {
    Q_OBJECT
public:
    about(QWidget *parent = 0);
    ~about();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::MainWindow *ui;
};

And the beginning of about.cpp: 而about.cpp的开头:

#include "about.h"
#include "ui_about.h"

about::about(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

I'm very sorry sir if I can just reply now after a long time not logging in because I have to finish my Face Recognition project in my college. 我很抱歉先生,如果我可以在很长一段时间没有登录后立即回复,因为我必须在我的大学完成我的面部识别项目。 By the way, I've solved my question after trying a lot of "Trials and Errors" and now I can do it well. 顺便说一句,我在尝试了很多“试炼和错误”之后解决了我的问题,现在我可以做得很好。 I've posted my project for the answer "How to call created .ui design form in Qt with button.clicked() event?" 我已经发布了我的项目答案“如何使用button.clicked()事件在Qt中调用创建的.ui设计表单?” in my personal blog. 在我的个人博客中。

This is the post link: http://hxr99.blogspot.com/2011/12/how-to-call-ui-design-form-with.html . 这是帖子链接: http//hxr99.blogspot.com/2011/12/how-to-call-ui-design-form-with.html There's a download link for the example project for calling the window,too. 还有一个用于调用窗口的示例项目的下载链接。 Maybe it can help people around the world beginning in Qt programming. 也许它可以帮助世界各地的人们从Qt编程开始。

I'm very thankful to you helping me solving my problem and sorry whether my English is not so good. 我非常感谢你帮助我解决我的问题,对不起我的英语是不是很好。 Merry Christmas and Happy New Year. 圣诞快乐和新年快乐。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM