开发者

Qt - What is meant by those lines of code

In the C++ GUI Programming with Qt 4 book, part of creating a dialog application, there was the following main.cpp file:

#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(d开发者_如何学Goialog);
dialog->show();
return app.exec();
}

Can you just describe those lines of code?

    Ui::GoToCellDialog ui;
    QDialog *dialog = new QDialog;
    ui.setupUi(dialog);

Thanks.


"ui_gotocelldialog.h" is a file generated automatically based on the file "gotocelldialog.ui" which contains the GUI for the dialog. Ui::GoToCellDialog::setupUi() must be called for the UI to be initialized.


Lets take a look:

Ui::GoToCellDialog ui;

This line creates an instance of GoToCellDialog. As already have been said, this class automatically generated from the gotocelldialog.ui file. The use-case is:

  1. Open qt-designer and make the interface you want.
  2. Save the file (in our case gotocelldialog.ui)
  3. In your .cpp file write #include "ui_gotocelldialog.h"
  4. Now you can use the interface you designed
  5. PROFIT????

Next:

QDialog *dialog = new QDialog;

This line creates new instance of the QDialog class that represents simple modal window (commonly called as dialog). But your window would be empty after this line. You need to place the controls, do you? How you can do this? Lets see:

ui.setupUi(dialog);

This line uses the interface you designed in the qt-designer. It places this interface to the newly created dialog. So you can see all the controls in the window. Pretty easy as for me.


It sets up the ui described by an qt ui xml file hosted inside of a dialog window/qdialog.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜