3

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.

anarchy99
  • 993
  • 4
  • 14
  • 20

1 Answers1

4

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);
}
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Daggerstab
  • 623
  • 5
  • 7
  • I'm very sorry for my late reply. By the way, thank you for your response. According to a similar post in (http://stackoverflow.com/questions/1518317/how-to-show-another-window-from-mainwindow-in-qt) and the example from (http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html). I have made a simple project to open an About Window connected with the pushButton. I've uploaded my project into (http://www.mediafire.com/?y6z0ub4f0k0fnkd). But, there still an error I can't figure out. would you like to see my project and explain me what can I do to solve it. I appreciate it very much. Thank you – anarchy99 Dec 18 '11 at 16:58
  • When sending archives to other people, please use more portable archive formats than RAR. ZIP for example is acceptable anywhere. – Daggerstab Dec 19 '11 at 09:08