Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Question on initialization of mainwindow

Question on initialization of mainwindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 822 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pdsc_dy
    wrote on last edited by A Former User
    #1

    I am learning a Qt example for Dock Widgets. In the .h file the mainwindow has a default constructor that does not have any arguments, though the the mainwindow includes a member called textEdit, along with other member widegets.

      MainWindow();
    

    ....
    QTextEdit *textEdit;
    ....

    However, in the implementation the mainwindow constructor includes textEdit in the initilizer list, even though it is not an argument for the mainwindow constructor. At the same time other member widgets are not included in the list. Can you help me understand this?

    MainWindow::MainWindow()
    : textEdit(new QTextEdit)
    {
    setCentralWidget(textEdit);
    .....

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      There are 4 pointers in that class: textEdit, customerList, paragraphsList and viewMenu. All of them need to be initialized somewhere.

      The first one is initialized in the initialization list of the class constructor as you mentioned. Using initialization list doesn't require you to init the member via a parameter. It can be initialized from any value, in this case it's a new statement. For example:

      class Foo {
      public:
          Foo() : bar(42) {}             //initializing bar via value
          Foo(int param) : bar(param) {} //initializing bar via param
          Foo() { initBar(); }           //initializing bar via member function
      private:
          void initBar() {  bar = 42; }
          int bar;
      };
      

      The example uses the first type of initialization for textEdit.

      All the other pointer members are initialized in the MainWindow::createDockWindows() which is called from the constructor's body. It's the third type of initialization from the example above.

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved