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. Constructor syntax in Qt in mainwindow.cpp
Forum Updated to NodeBB v4.3 + New Features

Constructor syntax in Qt in mainwindow.cpp

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 2.4k 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.
  • jsulmJ jsulm

    @Swati777999 Please read https://doc.qt.io/qt-5/objecttrees.html
    And also learn about C++ constructors.

    1. Parent is of type QWidget* because any widget can be parent of other widgets. "does this parent has same type with QMainWindow?" - why don't you check documentation (https://doc.qt.io/qt-5/qmainwindow.html#QMainWindow)?
    2. This has nothing to do with Qt. name, place, thing and about seem to be member variables in your MainWindow class which are initialised this way in constructor. This are C++ basics, has nothing to do with Qt.
    Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #3

    @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

    :QMainWindow(parent),
    name(0),
    place(0),
    thing(0),
    about(0)
    

    Generally, in C++ , any function is declared as
    void custom_func (arg1,arg2......argn)
    {
    ............
    }

    Please clarify the above syntax of Qt constructor declaration.

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    jsulmJ JonBJ KroMignonK 3 Replies Last reply
    0
    • Swati777999S Swati777999

      @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

      :QMainWindow(parent),
      name(0),
      place(0),
      thing(0),
      about(0)
      

      Generally, in C++ , any function is declared as
      void custom_func (arg1,arg2......argn)
      {
      ............
      }

      Please clarify the above syntax of Qt constructor declaration.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

      I don't understand how the member variables are represented in the declaration of the constructor with commas.

      Please learn C++! You are asking absolute basics!

      class SomeClass
      {
      public:
          SomeClass():
              a(1),
              b("Some String")
          {}
      private:
          int a;
          std::string b;
      };
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Swati777999S 1 Reply Last reply
      3
      • Swati777999S Swati777999

        @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

        :QMainWindow(parent),
        name(0),
        place(0),
        thing(0),
        about(0)
        

        Generally, in C++ , any function is declared as
        void custom_func (arg1,arg2......argn)
        {
        ............
        }

        Please clarify the above syntax of Qt constructor declaration.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #5

        @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

        Generally, in C++ , any function is declared as
        void custom_func (arg1,arg2......argn)
        {

        This is a plain C function declaration. The example you are asking about is for a C++ class method, and inheriting from a base class.

        It does not declare the variables in the comma-list, it merely initializes them. They must be declared as class members, as @jsulm has shown. In this particular case it is just equivalent to:

         MainWindow::MainWindow(QWidget *parent)
         : QMainWindow(parent)
        {
             name = 0;
             place = 0;
             thing = 0;
             about = 0;
            ...
        }
        
        Swati777999S 1 Reply Last reply
        2
        • Swati777999S Swati777999

          @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

          :QMainWindow(parent),
          name(0),
          place(0),
          thing(0),
          about(0)
          

          Generally, in C++ , any function is declared as
          void custom_func (arg1,arg2......argn)
          {
          ............
          }

          Please clarify the above syntax of Qt constructor declaration.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #6

          @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

          I don't understand how the member variables are represented in the declaration of the constructor with commas.

          As told by @jsulm : this is C++ basic knowledge: https://en.cppreference.com/w/cpp/language/constructor

          As Qt is a C++ framework, it will really help you, and save from frustrations, to to first learn C++ basics.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

            I don't understand how the member variables are represented in the declaration of the constructor with commas.

            Please learn C++! You are asking absolute basics!

            class SomeClass
            {
            public:
                SomeClass():
                    a(1),
                    b("Some String")
                {}
            private:
                int a;
                std::string b;
            };
            
            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #7

            @jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.

            I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            KroMignonK jsulmJ 2 Replies Last reply
            0
            • Swati777999S Swati777999

              @jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.

              I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #8

              @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

              have never found the syntax of C++ function like this before

              Because this is not a function but a class constructor declaration.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              Swati777999S 1 Reply Last reply
              0
              • Swati777999S Swati777999

                @jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.

                I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                syntax of C++ function like this before

                This syntax is only valid for C++ constructors, not normal functions/methods.
                A C++ course should actually cover this basic thing.
                Please follow the link provided by @KroMignon

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                  Generally, in C++ , any function is declared as
                  void custom_func (arg1,arg2......argn)
                  {

                  This is a plain C function declaration. The example you are asking about is for a C++ class method, and inheriting from a base class.

                  It does not declare the variables in the comma-list, it merely initializes them. They must be declared as class members, as @jsulm has shown. In this particular case it is just equivalent to:

                   MainWindow::MainWindow(QWidget *parent)
                   : QMainWindow(parent)
                  {
                       name = 0;
                       place = 0;
                       thing = 0;
                       about = 0;
                      ...
                  }
                  
                  Swati777999S Offline
                  Swati777999S Offline
                  Swati777999
                  wrote on last edited by
                  #10

                  @JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.

                  “ In order to be irreplaceable, one must always be different” – Coco Chanel

                  jsulmJ 1 Reply Last reply
                  0
                  • Swati777999S Swati777999

                    @JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                    This is how the variables are initialized.

                    This is a possibility. But in proper C++ projects this is not the way members are initialised. Such code would not get approved in projects I work on unless there is really a need to do it this way.
                    Correct way to initialise members is using the syntax you were asking about. So, I suggest you learn and use it. This is also nothing new, exists in C++ for ages already.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • KroMignonK KroMignon

                      @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                      have never found the syntax of C++ function like this before

                      Because this is not a function but a class constructor declaration.

                      Swati777999S Offline
                      Swati777999S Offline
                      Swati777999
                      wrote on last edited by
                      #12

                      @KroMignon Precisely, a constructor is a member function of a class.

                      “ In order to be irreplaceable, one must always be different” – Coco Chanel

                      jsulmJ KroMignonK 2 Replies Last reply
                      0
                      • Swati777999S Swati777999

                        @KroMignon Precisely, a constructor is a member function of a class.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                        a constructor is a member function of a class

                        It is a special member function of a class

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        jsulmJ 1 Reply Last reply
                        0
                        • Swati777999S Swati777999

                          @KroMignon Precisely, a constructor is a member function of a class.

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #14

                          @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                          Precisely, a constructor is a member function of a class.

                          I don't understand what your goal is?
                          You ask a question about very basic C++ syntax, for which you've got answer and pointer to C++ documentation to get more detailed explanation, but you don't seems to accept those responses.
                          Why?
                          Do I hurt you in any way?

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                            a constructor is a member function of a class

                            It is a special member function of a class

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @jsulm Here an explanation why this specual syntax is better:

                            class A
                            {
                            public:
                                A() { std::cout << "A" << std::endl; }
                                A(int) { std::cout << "A(int)" << std::endl; }
                            };
                            
                            class B
                            {
                            public:
                                B() { a = A(1); }
                            
                            private:
                                A a;
                            };
                            
                            B b;
                            

                            In the above code you will see that both A constructors are called, even though you are creating only one A instance explicetly. Reason is that members are initialised for you if you don't do it explicetly. In this case a is initialised using default constructor first, but then you create a new one using the other constructor and assign it to a.
                            If you change

                            B() { a = A(1); }
                            

                            to

                            B()
                                : a(1)
                            {  }
                            

                            you will see that only one constructor is called. So, it is more efficient to use the special constructor syntax.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            M 1 Reply Last reply
                            3
                            • jsulmJ jsulm

                              @jsulm Here an explanation why this specual syntax is better:

                              class A
                              {
                              public:
                                  A() { std::cout << "A" << std::endl; }
                                  A(int) { std::cout << "A(int)" << std::endl; }
                              };
                              
                              class B
                              {
                              public:
                                  B() { a = A(1); }
                              
                              private:
                                  A a;
                              };
                              
                              B b;
                              

                              In the above code you will see that both A constructors are called, even though you are creating only one A instance explicetly. Reason is that members are initialised for you if you don't do it explicetly. In this case a is initialised using default constructor first, but then you create a new one using the other constructor and assign it to a.
                              If you change

                              B() { a = A(1); }
                              

                              to

                              B()
                                  : a(1)
                              {  }
                              

                              you will see that only one constructor is called. So, it is more efficient to use the special constructor syntax.

                              M Offline
                              M Offline
                              mpergand
                              wrote on last edited by mpergand
                              #16

                              To go along with @jsulm
                              With const and reference variables, using the initializer list is mandatory.

                              class B
                              {
                              public:
                                  B() 
                                      { 
                                      a = A(1);
                                      str="hello";  // error
                                      }
                              
                              private:
                                  A a;
                                  const string str;
                              };
                              

                              Funny enough, that way you can initialize a const var two times !

                              class B
                              {
                              public:
                                  B() : str("hello2") 
                                      { 
                                      a = A(1);
                              
                                      }
                              
                              private:
                                  A a;
                                  const string str="hello";  // since  c++11
                              };
                              
                              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