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 Set QMainWindow Size?

How to Set QMainWindow Size?

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 6 Posters 55.1k 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
    Andaharoo
    wrote on last edited by
    #1

    How do I set the size of qmainwindow so it's exactly the size I want it to be and resizable? Also I don't want it to be set to a minimum or a maximum size. I've been googling for like 2 hours and every solution I tried didn't work.

    KillerSmathK 1 Reply Last reply
    0
    • A Andaharoo

      How do I set the size of qmainwindow so it's exactly the size I want it to be and resizable? Also I don't want it to be set to a minimum or a maximum size. I've been googling for like 2 hours and every solution I tried didn't work.

      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @Andaharoo
      Look the documentation. QMainWindow inherits QWidget.
      https://doc.qt.io/qt-5/qwidget.html

      You can set the Size of QMainWindow of 2 ways:

      1. Call resize function
      2. Call setGeometry function

      In relation to be resizable, you can change the Size Policy by calling:
      setSizePolicy

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      4
      • A Offline
        A Offline
        Andaharoo
        wrote on last edited by
        #3

        Neither resize or setgeometry do anything.

        Pradeep P NP 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Andaharoo said in How to Set QMainWindow Size?:

          Neither resize or setgeometry do anything.

          Since it is working for the rest of us I would guess you're doing something wrong - please provide a minimal testcase. What OS do you use?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • A Andaharoo

            Neither resize or setgeometry do anything.

            Pradeep P NP Offline
            Pradeep P NP Offline
            Pradeep P N
            wrote on last edited by
            #5

            @Andaharoo the resize event should work as suggested by @KillerSmath

            MainWindow w;
                w.resize(500,500);
                w.show();
            

            Pass the appropriate size as per your Widget in it.

            In case if you still have some issue with your code then please check QResizeEvent which can help you.
            https://doc.qt.io/qt-5/qresizeevent.html

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            1 Reply Last reply
            1
            • B Offline
              B Offline
              bsomervi
              wrote on last edited by
              #6

              This has not been satisfactorily answered. I have the same or similar issues. For example if a QMainWindow subclass has a central widget, with children and layout managers; then if widgets are hidden and variable size widgets like QTextEdit are visible it seems impossible to change the size of the window from code. The layouts seem to always take account of hidden widgets and grow the variable size visible widgets as a result. Is there some way to force the layout to respect the currently visible widgets and the size of the window? Even using layout ()->removeWidget(widget_ptr) before hiding the widgets doesn't seem to remove their influence on sizing!!

              Christian EhrlicherC 1 Reply Last reply
              1
              • B bsomervi

                This has not been satisfactorily answered. I have the same or similar issues. For example if a QMainWindow subclass has a central widget, with children and layout managers; then if widgets are hidden and variable size widgets like QTextEdit are visible it seems impossible to change the size of the window from code. The layouts seem to always take account of hidden widgets and grow the variable size visible widgets as a result. Is there some way to force the layout to respect the currently visible widgets and the size of the window? Even using layout ()->removeWidget(widget_ptr) before hiding the widgets doesn't seem to remove their influence on sizing!!

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @bsomervi Please provide a minimal, compileable example - it works by calling setSize() accordingly.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bsomervi
                  wrote on last edited by
                  #8

                  Will do shortly.

                  I am using C++ Widgets, what class is setSize a member of?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @bsomervi said in How to Set QMainWindow Size?:

                    what class is setSize a member of?

                    I meant QWidget::resize() which changes the size property

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bsomervi
                      wrote on last edited by bsomervi
                      #10

                      OK, so here's an example. The intention is to have two main window states, one showing two resizable widgets and the other only showing one. The idea is that the one widget display is a short-form version to save screen space. Build and run, size the main window to some large size, check the menu action then move and resize the single widget version, specifically make it as small as possible. From that point I want the menu action to flip between the two main window sizes, but it doesn't. The one widget size increases and that increase is something to do with the hidden widget.

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        bsomervi
                        wrote on last edited by
                        #11

                        To be specific, QWidget::resize() is ignoring the height I set even though the user can resize to that size using the mouse!

                        1 Reply Last reply
                        1
                        • B Offline
                          B Offline
                          bsomervi
                          wrote on last edited by bsomervi
                          #12

                          Sorry, messed up the repro example with last minute simplifications, this one actually builds and runs:

                          #include <QApplication>
                          #include <QMainWindow>
                          #include <QWidget>
                          #include <QTextEdit>
                          #include <QVBoxLayout>
                          #include <QAction>
                          #include <QMenuBar>
                          #include <QMenu>
                          
                          
                          class MainWindow
                            : public QMainWindow
                          {
                            Q_OBJECT
                          
                          public:
                            explicit MainWindow ()
                              : central_widget_ {new QWidget {this}}
                              , edit_ {new QTextEdit {central_widget_}}
                              , hideable_edit_ {new QTextEdit {central_widget_}}
                              , layout_ {new QVBoxLayout}
                              , resize_action_ {new QAction {"&Resize"}}
                            {
                              setCentralWidget (central_widget_);
                              layout_->addWidget (edit_);
                              layout_->addWidget (hideable_edit_);
                              central_widget_->setLayout (layout_);
                              resize_action_->setCheckable (true);
                              action_menu_ = menuBar ()->addMenu (tr ("&Action"));
                              action_menu_->addAction (resize_action_);
                              connect (resize_action_, &QAction::toggled, this, &MainWindow::do_resize);
                            }
                          
                          private:
                            Q_SLOT void do_resize (bool checked)
                            {
                              auto s = size ();
                              hideable_edit_->setVisible (!checked);
                              if (saved_size_.isValid ())
                                {
                                  // here I want to have the user resizeable main window flipped
                                  // as if the lower QTextEdit just disappeared/reappeared, i.e. the top
                                  // QTextEdit remains exactly as is (i.e. as resized by the
                                  // user resizing the main window)
                                  //
                                  // I want the user to be able to move and resize the main
                                  // window in either state and then the menu action flips
                                  // between them. It doesn't restore the size if the user
                                  // shrinks the main window below a certain size.
                                  resize (saved_size_);
                                }
                              saved_size_ = s;          // save for when we flip back
                            }
                          
                            QWidget * central_widget_;
                            QTextEdit * edit_;
                            QTextEdit * hideable_edit_;
                            QVBoxLayout * layout_;
                            QAction * resize_action_;
                            QMenu * action_menu_;
                            QSize saved_size_;
                          };
                          
                          int main (int argc, char * argv[])
                          {
                            QApplication app {argc, argv};
                            MainWindow main_window;
                            main_window.show ();
                            QApplication::connect (&app, &QApplication::lastWindowClosed, &app, &QApplication::quit);
                            return app.exec ();
                          }
                          
                          #include "main.moc"
                          
                          Cobra91151C 1 Reply Last reply
                          0
                          • B bsomervi

                            Sorry, messed up the repro example with last minute simplifications, this one actually builds and runs:

                            #include <QApplication>
                            #include <QMainWindow>
                            #include <QWidget>
                            #include <QTextEdit>
                            #include <QVBoxLayout>
                            #include <QAction>
                            #include <QMenuBar>
                            #include <QMenu>
                            
                            
                            class MainWindow
                              : public QMainWindow
                            {
                              Q_OBJECT
                            
                            public:
                              explicit MainWindow ()
                                : central_widget_ {new QWidget {this}}
                                , edit_ {new QTextEdit {central_widget_}}
                                , hideable_edit_ {new QTextEdit {central_widget_}}
                                , layout_ {new QVBoxLayout}
                                , resize_action_ {new QAction {"&Resize"}}
                              {
                                setCentralWidget (central_widget_);
                                layout_->addWidget (edit_);
                                layout_->addWidget (hideable_edit_);
                                central_widget_->setLayout (layout_);
                                resize_action_->setCheckable (true);
                                action_menu_ = menuBar ()->addMenu (tr ("&Action"));
                                action_menu_->addAction (resize_action_);
                                connect (resize_action_, &QAction::toggled, this, &MainWindow::do_resize);
                              }
                            
                            private:
                              Q_SLOT void do_resize (bool checked)
                              {
                                auto s = size ();
                                hideable_edit_->setVisible (!checked);
                                if (saved_size_.isValid ())
                                  {
                                    // here I want to have the user resizeable main window flipped
                                    // as if the lower QTextEdit just disappeared/reappeared, i.e. the top
                                    // QTextEdit remains exactly as is (i.e. as resized by the
                                    // user resizing the main window)
                                    //
                                    // I want the user to be able to move and resize the main
                                    // window in either state and then the menu action flips
                                    // between them. It doesn't restore the size if the user
                                    // shrinks the main window below a certain size.
                                    resize (saved_size_);
                                  }
                                saved_size_ = s;          // save for when we flip back
                              }
                            
                              QWidget * central_widget_;
                              QTextEdit * edit_;
                              QTextEdit * hideable_edit_;
                              QVBoxLayout * layout_;
                              QAction * resize_action_;
                              QMenu * action_menu_;
                              QSize saved_size_;
                            };
                            
                            int main (int argc, char * argv[])
                            {
                              QApplication app {argc, argv};
                              MainWindow main_window;
                              main_window.show ();
                              QApplication::connect (&app, &QApplication::lastWindowClosed, &app, &QApplication::quit);
                              return app.exec ();
                            }
                            
                            #include "main.moc"
                            
                            Cobra91151C Offline
                            Cobra91151C Offline
                            Cobra91151
                            wrote on last edited by Cobra91151
                            #13

                            @bsomervi

                            Hello!
                            You can try to use the restoreGeometry method - https://doc.qt.io/qt-5/qwidget.html#restoreGeometry

                            Here is my example (main.cpp):

                            #include <QApplication>
                            #include <QWidget>
                            #include <QMainWindow>
                            #include <QPushButton>
                            
                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                                QMainWindow window;
                                window.setWindowTitle("Test MainWindow");
                                QWidget widget;
                                widget.setMinimumSize(400, 300);
                                window.setCentralWidget(&widget);
                                QPushButton *testBtn = new QPushButton("Restore size", &window);
                                testBtn->setFixedSize(150, 100);
                                window.show();
                                QByteArray savedGeometry = window.saveGeometry();
                                QObject::connect(testBtn, &QPushButton::clicked, [&window, savedGeometry]() {
                                    if (!savedGeometry.isNull() && !savedGeometry.isEmpty()) {
                                        window.restoreGeometry(savedGeometry);
                                    } else {
                                        window.setGeometry(window.geometry().x(), window.geometry().y(), 400, 300);
                                    }
                                });
                                return a.exec();
                            }
                            

                            Result:

                            Restore geometry example

                            Happy coding!

                            1 Reply Last reply
                            0
                            • B Offline
                              B Offline
                              bsomervi
                              wrote on last edited by
                              #14

                              Sure I have tried using (save|restore)Geometry() they have exactly the same issue. If you can make them work with my example then I would be delighted, but I've had no luck.

                              Cobra91151C 1 Reply Last reply
                              0
                              • B bsomervi

                                Sure I have tried using (save|restore)Geometry() they have exactly the same issue. If you can make them work with my example then I would be delighted, but I've had no luck.

                                Cobra91151C Offline
                                Cobra91151C Offline
                                Cobra91151
                                wrote on last edited by
                                #15

                                @bsomervi

                                I have tried your code, fixed a few compile errors, but for me it works well.

                                Result:
                                Restore example

                                What are you trying to achieve? By the way, you can use QSplitter https://doc.qt.io/qt-5/qsplitter.html with layout to hide the hideable_edit_ (QTextEdit).

                                Also, there are two options you can try:

                                1. Use QResizeEvent https://doc.qt.io/qt-5/qresizeevent.html
                                2. Use nativeEvent https://doc.qt.io/qt-5/qwidget.html#nativeEvent to intercept and handle the resize/restore window messages (https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-size, https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-sizing)
                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  bsomervi
                                  wrote on last edited by bsomervi
                                  #16

                                  The problem is when the single widget rendering is shrunk using the mouse to the minimum size toggling the action twice does not restore the main window size. I can find no way to set the size of the main window as small as it was settable using the mouse. This is a problem (a) because there seems to be no way to do in code what can be done with the mouse, and (b) the whole point is to have a "view" of the main window set by the user that can be returned to using the code. Users simply cannot believe we can't do this and their chosen size is ignored.

                                  1 Reply Last reply
                                  0
                                  • B Offline
                                    B Offline
                                    bsomervi
                                    wrote on last edited by bsomervi
                                    #17

                                    native events are no use to us as our application is portable. How do i use resizeEvents to do this? Note that my real case has a whole bunch of widgets and layouts in the hideable part, I don't think a splitter is appropriate as the bottom part is not resizable. I just used another QTextEdit in my example above as the header was already included.

                                    Cobra91151C 1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      bsomervi
                                      wrote on last edited by
                                      #18

                                      @Cobra91151 where are the compile errors in my code example?

                                      You second animation above clearly shows the issue I am trying to solve.

                                      How do I make use of an override resizeEvent() to fix this issue? I don't see how it can help, a resize Event is triggered by some external action, how can I make it fix the specific when a call to MainWindow::resize() does not do what it is asked to do?

                                      Cobra91151C 1 Reply Last reply
                                      0
                                      • B bsomervi

                                        native events are no use to us as our application is portable. How do i use resizeEvents to do this? Note that my real case has a whole bunch of widgets and layouts in the hideable part, I don't think a splitter is appropriate as the bottom part is not resizable. I just used another QTextEdit in my example above as the header was already included.

                                        Cobra91151C Offline
                                        Cobra91151C Offline
                                        Cobra91151
                                        wrote on last edited by
                                        #19

                                        @bsomervi

                                        What do you mean portable? I suppose your app must run on different operating systems? If so, then check out this link: https://forum.qt.io/topic/104533/how-to-ignore-the-event-when-i-click-on-the-button-to-maximize-the-window-windowmaximizebuttonhint/9

                                        You can use nativeEvent with defines for different operating systems and add the appropriate code.

                                        1 Reply Last reply
                                        0
                                        • B bsomervi

                                          @Cobra91151 where are the compile errors in my code example?

                                          You second animation above clearly shows the issue I am trying to solve.

                                          How do I make use of an override resizeEvent() to fix this issue? I don't see how it can help, a resize Event is triggered by some external action, how can I make it fix the specific when a call to MainWindow::resize() does not do what it is asked to do?

                                          Cobra91151C Offline
                                          Cobra91151C Offline
                                          Cobra91151
                                          wrote on last edited by Cobra91151
                                          #20

                                          @bsomervi

                                          As for compile errors: you forgot to add & in two connects. Example below:

                                          Your code:
                                          connect(resize_action_, QAction::toggled, this, &MainWindow::do_resize);
                                          QApplication::connect(&app, QApplication::lastWindowClosed, &app, QApplication::quit);

                                          Fixed:
                                          connect(resize_action_, &QAction::toggled, this, &MainWindow::do_resize);
                                          QApplication::connect(&app, &QApplication::lastWindowClosed, &app, QApplication::quit);

                                          1 Reply Last reply
                                          1

                                          • Login

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