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 Update on Monday, May 27th 2025

Class Constructor

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 4.0k Views
  • 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.
  • A Offline
    A Offline
    a8wz
    wrote on 23 Feb 2015, 16:03 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Feb 2015, 16:13 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
      • A Offline
        A Offline
        a8wz
        wrote on 23 Feb 2015, 17:14 last edited by
        #3

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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 23 Feb 2015, 17:35 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

          1/4

          23 Feb 2015, 16:03

          • Login

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