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. How to use MainWindow centralWidget & Layout
QtWS25 Last Chance

How to use MainWindow centralWidget & Layout

Scheduled Pinned Locked Moved Solved General and Desktop
central widgetmainwindow
7 Posts 4 Posters 26.9k 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.
  • B Offline
    B Offline
    Burke212
    wrote on last edited by
    #1

    I prefer to do my development in source. I've read many articles and watched many tutorials, but I still don't completely understand MainWindow's built-in centralWidget & layout, and how to use them.

    What I end up doing is creating my own QWidget mainWidget and mainLayout.

    Is this the correct way, or should I utilize the built-in objects? How do I properly use them?

    Below is an example of how I typically setup my apps:

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QWidget>
    #include <QDebug>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        void setup();
    
        QPushButton *button;
        QVBoxLayout *mainLayout;
        QWidget *mainWidget;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        setup();
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::setup(){
        button = new QPushButton();
        button->setText("Press Me");
    
        mainLayout = new QVBoxLayout();
        mainLayout->addWidget(button);
    
        mainWidget = new QWidget();
        mainWidget->setLayout(mainLayout);
        mainWidget->show();
    }
    
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
    //    w.show();
    
        return a.exec();
    }
    
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2
      mainWidget->show();
      

      Wrong, this is the central wiget of the MainWindow:

      setCentralWidget(mainWidget);
      

      and show the mainWindow instead in main:

      MainWindow w;
      w.show();
      

      And all should be good :)

      B 1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Just as a note.
        The default CentralWidget() for a default GUI app (with UI file) is simply just a plain QWidget.
        So there is no special trick or special feature with it.
        Using setCentralWidget(mainWidget); simply sets it and
        there is nothing more to consider.

        1 Reply Last reply
        3
        • M mpergand
          mainWidget->show();
          

          Wrong, this is the central wiget of the MainWindow:

          setCentralWidget(mainWidget);
          

          and show the mainWindow instead in main:

          MainWindow w;
          w.show();
          

          And all should be good :)

          B Offline
          B Offline
          Burke212
          wrote on last edited by
          #4

          @mpergand & @mrjj ,

          So it isn't wrong to create and use my own widget as the main widget? It just seems to me that since Qt has a built in centralWidget that I should utilize it instead of recreating the wheel with another widget.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            By default the centralWidget contains nothing ( centralWidget() return null ), hence if you create your UI by code, you must set the centralWidget with your own widget.

            If you build your UI with the Designer, the central widget is created automaticaly when the Ui is build ( ui->setupUi(this) )

            Extract from some code of mine:

            QTLog(<<centralWidget())
            ui->setupUi(this);
            QTLog(<<centralWidget())
            

            logs:
            QWidget(0x0)
            QWidget(0x7faf58dac750, name="centralWidget")

            B 1 Reply Last reply
            5
            • M mpergand

              By default the centralWidget contains nothing ( centralWidget() return null ), hence if you create your UI by code, you must set the centralWidget with your own widget.

              If you build your UI with the Designer, the central widget is created automaticaly when the Ui is build ( ui->setupUi(this) )

              Extract from some code of mine:

              QTLog(<<centralWidget())
              ui->setupUi(this);
              QTLog(<<centralWidget())
              

              logs:
              QWidget(0x0)
              QWidget(0x7faf58dac750, name="centralWidget")

              B Offline
              B Offline
              Burke212
              wrote on last edited by
              #6

              @mpergand

              Ah, I understand.

              centralWidget is blank, and Qt sort of expects you to use the Designer, which will automatically modify when you start dragging and dropping elements onto it.

              So in source I simply use setCentralWidget(centralWidget), and add elements and layouts to design the UI. That makes sense.

              So then, if I set and populate centralWidget with all of the elements the UI will need, do I even need to create another "main widget"? Or are you saying that I do need to create another main/central widget, because centralWidget doesn't even have a widget to add other elements to?

              JonBJ 1 Reply Last reply
              0
              • B Burke212

                @mpergand

                Ah, I understand.

                centralWidget is blank, and Qt sort of expects you to use the Designer, which will automatically modify when you start dragging and dropping elements onto it.

                So in source I simply use setCentralWidget(centralWidget), and add elements and layouts to design the UI. That makes sense.

                So then, if I set and populate centralWidget with all of the elements the UI will need, do I even need to create another "main widget"? Or are you saying that I do need to create another main/central widget, because centralWidget doesn't even have a widget to add other elements to?

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

                @Burke212
                Given that you are not using the designer, create your own QWidget *mainWidget and at some point call setCentralWidget(mainWidget). You can add your widgets onto your mainWidget before or after setting it as your central widget, I don't think it matters.

                So just one "main widget", which you create, and then you set the main window to have that as its central widget.

                1 Reply Last reply
                2

                • Login

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