开发者

Qt C++ Initialization List Confusion

开发者 https://www.devze.com 2023-04-05 13:21 出处:网络
I\'m getting started with Qt (and C++, to a lesser extent), and I wanted to be sure I fully understood the base code before continuing on. I understand that the fir开发者_如何学Gost element in the ini

I'm getting started with Qt (and C++, to a lesser extent), and I wanted to be sure I fully understood the base code before continuing on. I understand that the fir开发者_如何学Gost element in the initialization list is being used to select a non-default inherited constructor.

What is the purpose of ui(new Ui::TestAppMain), though? It seems to me like it would be an infinite loop, since ui is being set to a new instance of TestAppMain in the constructor, but that's not the case.

namespace Ui {
    class TestAppMain;
}

class TestAppMain : public QMainWindow{
    public:
        explicit TestAppMain(QWidget *parent = 0);

    private:
        Ui::TestAppMain *ui;
};

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


Ui::TestAppMain is not the same as your TestAppMain class. It's another C++ class that is generated by Qt from the .ui file you've created in Qt Creator. To avoid confusion and naming conflicts, Qt puts all such generated classes in the Ui namespace.

It is a standard Qt practice to include an instance of Ui::MyWidget in your own class MyWidget. In your case, after you instantiate Ui::TestAppMain, you use that object to initialize your main window (represented by your TestAppMain class) according to the layout you've specified in TestAppMain.ui. That is done by calling ui->setupUi(this).

0

精彩评论

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