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. Qt Basics and Selection of Base class

Qt Basics and Selection of Base class

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 1.2k 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    Hi All ,

    I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.

    See the following example:

    main.cpp

    #include "mylayout.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        myLayout w;
        //w.show();
        return a.exec();
    }
    
    

    mainLayout.h

    #ifndef MYLAYOUT_H
    #define MYLAYOUT_H
    
    #include <QMainWindow>
    
    class myLayout : public QMainWindow
    {
        Q_OBJECT
    
    public:
        myLayout(QWidget *parent = nullptr);
        ~myLayout();
    };
    #endif // MYLAYOUT_H
    

    mainLayout.cpp

    #include "mylayout.h"
    #include <QWidget>
    #include <QLabel>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QSpinBox>
    
    myLayout::myLayout(QWidget *parent)
        : QMainWindow(parent)
    {
    ..................................................................
    ..................................................................
    .......................................
    ...............................
    ...........................
    .....................
    ..............
    ........
    ......
    .....
    ...
    ..
    .
    
    }
    
    myLayout::~myLayout()
    {
    }
    

    In main.cpp, I had to comment w.show as I was getting an extra blank window and the implementation of mainlayout.cpp was shown in another window. So, I commented this redundant window.

    In the above case, the base class is QMainWindow. What differences will I get ,if I select the base class to be QWidget, apart from the advantages of the respective class properties and functions?

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

    JKSHJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      Hi All ,

      I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.

      See the following example:

      main.cpp

      #include "mylayout.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          myLayout w;
          //w.show();
          return a.exec();
      }
      
      

      mainLayout.h

      #ifndef MYLAYOUT_H
      #define MYLAYOUT_H
      
      #include <QMainWindow>
      
      class myLayout : public QMainWindow
      {
          Q_OBJECT
      
      public:
          myLayout(QWidget *parent = nullptr);
          ~myLayout();
      };
      #endif // MYLAYOUT_H
      

      mainLayout.cpp

      #include "mylayout.h"
      #include <QWidget>
      #include <QLabel>
      #include <QVBoxLayout>
      #include <QHBoxLayout>
      #include <QSpinBox>
      
      myLayout::myLayout(QWidget *parent)
          : QMainWindow(parent)
      {
      ..................................................................
      ..................................................................
      .......................................
      ...............................
      ...........................
      .....................
      ..............
      ........
      ......
      .....
      ...
      ..
      .
      
      }
      
      myLayout::~myLayout()
      {
      }
      

      In main.cpp, I had to comment w.show as I was getting an extra blank window and the implementation of mainlayout.cpp was shown in another window. So, I commented this redundant window.

      In the above case, the base class is QMainWindow. What differences will I get ,if I select the base class to be QWidget, apart from the advantages of the respective class properties and functions?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @Swati777999 said in Qt Basics and Selection of Base class:

      I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.

      QWidget is the base class of QMainWindow and QDialog. That means:

      • QMainWindow can do everything that QWidget can do. It adds features like a menu bar, status bar, and dockable widgets.
      • QDialog can do everything that QWidget can do. It is designed for temporary pop-up windows.

      So, if you want a temporary window, use QDialog. If you want a menu bar or status bar or dockable widgets, use QMainWindow. If you don't want any of these, use QWidget.

      I was getting an extra blank window and the implementation of mainlayout.cpp was shown in another window.

      That means your code in mainlayout.cpp has errors. You should fix that.

      You should NOT comment out w.show()!

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      Swati777999S 2 Replies Last reply
      2
      • JKSHJ JKSH

        @Swati777999 said in Qt Basics and Selection of Base class:

        I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.

        QWidget is the base class of QMainWindow and QDialog. That means:

        • QMainWindow can do everything that QWidget can do. It adds features like a menu bar, status bar, and dockable widgets.
        • QDialog can do everything that QWidget can do. It is designed for temporary pop-up windows.

        So, if you want a temporary window, use QDialog. If you want a menu bar or status bar or dockable widgets, use QMainWindow. If you don't want any of these, use QWidget.

        I was getting an extra blank window and the implementation of mainlayout.cpp was shown in another window.

        That means your code in mainlayout.cpp has errors. You should fix that.

        You should NOT comment out w.show()!

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

        @JKSH said in Qt Basics and Selection of Base class:

        That means your code in mainlayout.cpp has errors. You should fix that.
        You should NOT comment out w.show()!

        I now have uncommented w.show().

        CASE 1 - when the base class is QMainWindow

        mainLayout.cpp

        #include "mylayout.h"
        #include <QWidget>
        #include <QLabel>
        #include <QVBoxLayout>
        #include <QHBoxLayout>
        #include <QSpinBox>
        
        myLayout::myLayout(QWidget *parent)
            : QMainWindow(parent)
        {
        QWidget *primWin= new QWidget();  //Primary Window
        ..................................................................
        .......................................
        ...............................
        ...........................
        .....................
        ..............
        ........
        ......
        .....
        ...
        primWin->setWindowTitle("My Layout");
            primWin->show();   ..................................................LINE-1
        
        }
        
        myLayout::~myLayout()
        {
        }
        
        

        main.cpp

        #include "mylayout.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            myLayout w;
            w.show();.....................................LINE -2
            return a.exec();
        }
        
        

        RESULT :-

        From the above code, I am getting 2 windows ; one corresponding to LINE-1 having the title My Layout with desired implementation resulting from mainlayout.cpp ; second one corresponding to LINE-2 having title as the default title of the project mainlayout which is blank.

        CASE 2 - When the base class is QWidget

        CASE 3 - When the base class is QDialog

        For both cases , 2 and 3 , the result obtained is the same as CASE 1 .

        I want to get only one window ;that is window-1.

        How can I achieve that without commenting w.show() ?

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

        jsulmJ 1 Reply Last reply
        0
        • Swati777999S Swati777999

          @JKSH said in Qt Basics and Selection of Base class:

          That means your code in mainlayout.cpp has errors. You should fix that.
          You should NOT comment out w.show()!

          I now have uncommented w.show().

          CASE 1 - when the base class is QMainWindow

          mainLayout.cpp

          #include "mylayout.h"
          #include <QWidget>
          #include <QLabel>
          #include <QVBoxLayout>
          #include <QHBoxLayout>
          #include <QSpinBox>
          
          myLayout::myLayout(QWidget *parent)
              : QMainWindow(parent)
          {
          QWidget *primWin= new QWidget();  //Primary Window
          ..................................................................
          .......................................
          ...............................
          ...........................
          .....................
          ..............
          ........
          ......
          .....
          ...
          primWin->setWindowTitle("My Layout");
              primWin->show();   ..................................................LINE-1
          
          }
          
          myLayout::~myLayout()
          {
          }
          
          

          main.cpp

          #include "mylayout.h"
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              myLayout w;
              w.show();.....................................LINE -2
              return a.exec();
          }
          
          

          RESULT :-

          From the above code, I am getting 2 windows ; one corresponding to LINE-1 having the title My Layout with desired implementation resulting from mainlayout.cpp ; second one corresponding to LINE-2 having title as the default title of the project mainlayout which is blank.

          CASE 2 - When the base class is QWidget

          CASE 3 - When the base class is QDialog

          For both cases , 2 and 3 , the result obtained is the same as CASE 1 .

          I want to get only one window ;that is window-1.

          How can I achieve that without commenting w.show() ?

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

          @Swati777999 said in Qt Basics and Selection of Base class:

          From the above code, I am getting 2 windows

          Of course you get two windows, because you're creating two!
          myLayout is already a window - why do you create another one in its constructor?
          If you create a QWidget (or a derived class) without a parent and show it it becomes a window (as explained in documentation).
          Remove

          QWidget *primWin= new QWidget();
          

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

          1 Reply Last reply
          2
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @Swati777999 said in Qt Basics and Selection of Base class:

            How can I achieve that without commenting w.show() ?

            By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)

            Swati777999S 1 Reply Last reply
            1
            • C ChrisW67

              @Swati777999 said in Qt Basics and Selection of Base class:

              How can I achieve that without commenting w.show() ?

              By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)

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

              @ChrisW67 said in Qt Basics and Selection of Base class:

              By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)

              @jsulm Have a look at it.
              In the mainlayout constructor , I made following changes:
              QWidget *primWin = new QWidget(this);
              Then added ,
              QMainWindow::setCentralWidget(primaryWindow);
              and removed show() function for primWin.

              Yes, it worked for me.
              Thanks. :)

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

              JKSHJ 1 Reply Last reply
              0
              • Swati777999S Swati777999

                @ChrisW67 said in Qt Basics and Selection of Base class:

                By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)

                @jsulm Have a look at it.
                In the mainlayout constructor , I made following changes:
                QWidget *primWin = new QWidget(this);
                Then added ,
                QMainWindow::setCentralWidget(primaryWindow);
                and removed show() function for primWin.

                Yes, it worked for me.
                Thanks. :)

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @Swati777999 said in Qt Basics and Selection of Base class:

                Yes, it worked for me.
                Thanks. :)

                Great!

                Can you now explain why it works?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                Swati777999S 1 Reply Last reply
                0
                • JKSHJ JKSH

                  @Swati777999 said in Qt Basics and Selection of Base class:

                  I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.

                  QWidget is the base class of QMainWindow and QDialog. That means:

                  • QMainWindow can do everything that QWidget can do. It adds features like a menu bar, status bar, and dockable widgets.
                  • QDialog can do everything that QWidget can do. It is designed for temporary pop-up windows.

                  So, if you want a temporary window, use QDialog. If you want a menu bar or status bar or dockable widgets, use QMainWindow. If you don't want any of these, use QWidget.

                  I was getting an extra blank window and the implementation of mainlayout.cpp was shown in another window.

                  That means your code in mainlayout.cpp has errors. You should fix that.

                  You should NOT comment out w.show()!

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

                  @JKSH
                  If I take base class to be QDialog, I noticed following occurences:

                  • I get a question mark (instead of minimize option)in the title bar of the main window .

                  • The content of the widget occupies a small portion of the window and no longer occupies the entire window when the window is dragged to a larger area.

                  Can you explain me the reason?

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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #9

                    I assume you mean the top-level window is a QDialog.

                    With regard to the minimize button, what you are describing is the default window decorations of a dialog on your platform.

                    QMainWIndow manages its centralWidget() so it always occupies the entire space inside the window after menu bars, toolbars, status bar and potential docked widgets are taken out. This is an extension of the standard QWidget behaviour.

                    QDialog uses a layout manager to control its contents; this is the standard QWidget mechanism. If you do not apply a layout with the content widgets added to it then the content widgets are uncontrolled. They will appear on top of and inside their parent QWidget but will not be size managed by it.

                    1 Reply Last reply
                    1
                    • D Offline
                      D Offline
                      DavidJoseph
                      Banned
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • JKSHJ JKSH

                        @Swati777999 said in Qt Basics and Selection of Base class:

                        Yes, it worked for me.
                        Thanks. :)

                        Great!

                        Can you now explain why it works?

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

                        @JKSH said in Qt Basics and Selection of Base class:

                        @Swati777999 said in Qt Basics and Selection of Base class:

                        Yes, it worked for me.
                        Thanks. :)

                        Great!

                        Can you now explain why it works?

                        this points to the parent object of the constructor. To display the contents of primWin widget , it has to be set as the central widget of the Qmainwindow.

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

                        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