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. Class Constructor
Forum Updated to NodeBB v4.3 + New Features

Class Constructor

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 4.0k 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.
  • a8wzA Offline
    a8wzA Offline
    a8wz
    wrote on last edited by
    #1

    Could somebody please explain the syntax for this constructor, I mean what does it accomplish exactly. Sorry I am new to Qt and C++ and I've already read the docs but still need some clarification. As far as I understand MainWindow here declares QWidget as its parent, and the single column (":") is used for Initialization lists but I don't quite understand what this particular Initialization list does (especially the QMainWindow(parent) part, does it provide inheritance for MainWindow from QMainWindow class?). Thanks in advance.

    @MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      @
      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent) << call base class constructor passing parent
      , ui(new Ui::MainWindow) << instanciate a new Ui::MainWindow object and assign it to the ui variable (well assign the pointer to the newly created object to ui which is a pointer to a Ui::MainWindow object)@

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • a8wzA Offline
        a8wzA Offline
        a8wz
        wrote on last edited by
        #3

        Thank you, it did, but can you also please tell me why it is needed.

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

          Simple example :
          @
          class A {
          A() { ... }
          A(Type param) { ... }
          };

          class B : public A {
          B(Type param) {} //this will call A() implicitly
          B(Type param) : A(param) {} //this will call A(param)
          };
          @
          So it's needed to pass the parent pointer to the base class QObject so that it can do its magic with it. Otherwise the parent parameter would be ignored.

          1 Reply Last reply
          0

          • Login

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