简体   繁体   中英

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.

What I've done: 1. Created a new .ui design form (Forms.right click -> Add New -> Qt Designer Form) to the current project. 2. Designed an about dialog in the .ui form created. 3. Make a slot PushButton.clicked() in the MainWindow.

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.

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

Summary: The easiest way to handle an .ui file is to run it through uic (the User Interface Compiler) at compile time. This is done automatically if the .ui file is included in your project (.pro) file. ("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. Its name should be something like "ui_nameoftheoriginaluifile.hpp". Of course, after that you need to instantiate the form defined in the .hpp file.

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. 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". (Which is determined by the name of the form you have used in the .ui file.)

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. 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.

You also shouldn't be using a QMainWindow for an about dialog. QMainWindow is supposed to be the main window of your application - there shouldn't be any more instances of it.

So, 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:

#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?" in my personal blog.

This is the post link: 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.

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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