Results 1 to 5 of 5

Thread: Puzzling class member and its usage in class constructor

  1. #1
    Join Date
    Jun 2015
    Posts
    2

    Post Puzzling class member and its usage in class constructor

    I inherited a piece of code as the following:

    In the header file:

    namespace Ui
    {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    ...
    }

    In cpp file, the class constructor as defined as below:
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    Question is why a pointer to itself in the header file? Is this not just "this" as in C++?

    Then in the constructor, ui(new Ui::MainWindow) is used in the constructor. What does it do to the constructor?
    Last edited by Karen; 15th June 2015 at 18:56.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Puzzling class member and its usage in class constructor

    Quote Originally Posted by Karen View Post
    Question is why a pointer to itself in the header file? Is this not just "this" as in C++?

    Then in the constructor, ui(new Ui::MainWindow) is used in the constructor. What does it do to the constructor?
    This code is generated by the Qt meta object system.

    Look at your ui_MainWindow.h file that is generated by qmake and you'll see this is generated for the form you have created in the designer.

  3. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    256
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Puzzling class member and its usage in class constructor

    Nothing puzzling here. MainWindow is not the same as Ui::MainWindow. There is ui_mainwindow.h header somewhere around which contains the definition of Ui::MainWindow. The header should be either included in the header of your snippet, or the ctor includes the MOC file. You will often see this kind of coding in Qt. Please, accept.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Puzzling class member and its usage in class constructor

    The class Ui::MainWindow is the generated implementation or your Designer form (mainwindow.ui -> uic -> ui_mainwindow.h). An instance of that class is maintained privately by the form class MainWindow (which lives in the global namespace and not the Ui namespace). The Ui::mainWindow instance is created on the heap in the form class constructor initialization list. This is standard C++, nothing sneaky about it.

  5. #5
    Join Date
    Jun 2015
    Posts
    2

    Default Re: Puzzling class member and its usage in class constructor

    Thank you all for replies. Yes, indeed I found the Ui::MainWindow definition in ui_mainwindow.h that's generated by Qt ui compiler. That answered my question.

Similar Threads

  1. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 14:53
  2. Replies: 4
    Last Post: 29th May 2010, 12:56
  3. class constructor
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2008, 18:25
  4. default parameters in constructor class
    By mickey in forum General Programming
    Replies: 4
    Last Post: 23rd February 2008, 18:44
  5. Replies: 5
    Last Post: 14th July 2006, 22:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.