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 find out the initial size of a widget?

How to find out the initial size of a widget?

Scheduled Pinned Locked Moved Solved General and Desktop
initializationsizemaximized
9 Posts 4 Posters 1.4k 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.
  • D Offline
    D Offline
    dave2
    wrote on 11 Dec 2022, 20:31 last edited by
    #1

    My program has class MyMainWindow (derived from QMainWindow). MyMainWindow contains a few widgets, which I have laid out with Qt Creator's tool Design.

    I want MyMainWindow to start up as maximized, so the constructor looks like this:

    MyMainWindow::MyMainWindow() :
    QMainWindow(nullptr),
    ui(new Ui::MyMainWindow())
    {
    ui->setupUi(this);
    [other setup code]
    showMaximized();
    }

    This works: MyMainWindow starts as maximized, and the layout is as expected.

    Now, I would like to make some adjustments to the logic of my program based on the exact initial size of a certain widget inside MyMainWindow. How can I do that?

    By reimplementing QMainWindow::resizeEvent() and QMainWindow::showEvent():

    void MyMainWindow::resizeEvent(QResizeEvent* event)
    {
        [some logging code]
        QMainWindow::resizeEvent(event);
    }
    
    void MyMainWindow::showEvent(QShowEvent* event)
    {
        [some logging code]
        QMainWindow::showEvent(event);
    }
    

    I found out that:

    • showEvent() is called once at start-up, but not with the maximized size
    • resizeEvent() is called twice at start-up: the 1st time not with the maximized size, the 2nd time with the maximized size

    So, in principle it would be possible to achieve my goal by reacting to the 2nd call to MyMainWindow::resizeEvent(). How reliable would that method be? Could it depend on the Qt version? Would it also work on other platforms (Linux for example)? Is there a better way to do this?

    My environment: Qt 6, Windows 10, Microsoft Visual Studio 2019.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 12 Dec 2022, 20:36 last edited by
      #7

      Hi, another approach is to delay those business decisions/changes until after MainWindow's constructor is done (and the QWidgets have all their resizing done) say via a lambda, include "qtimer.h" and

      ...
      ui->setupUi(this);
      [other setup code]
      showMaximized();
      ...
      QTimer::singleShot(20,this,[this]
      {
      // ... the rest of the startup code, i.e. business decisions
      });
      // end of MainWindow constructor
      

      20 ms is usually enough time to allow all the QWidgets to resize twice properly.

      D 2 Replies Last reply 13 Dec 2022, 07:04
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 11 Dec 2022, 21:05 last edited by
        #2

        Hi,

        You can use the changeEvent and react on the event to get the size in the state of interest.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 2 Replies Last reply 12 Dec 2022, 08:14
        1
        • S SGaist
          11 Dec 2022, 21:05

          Hi,

          You can use the changeEvent and react on the event to get the size in the state of interest.

          D Offline
          D Offline
          dave2
          wrote on 12 Dec 2022, 08:14 last edited by
          #3

          @SGaist Thank you for the hint. I will try that.

          J 1 Reply Last reply 12 Dec 2022, 15:12
          0
          • D dave2
            12 Dec 2022, 08:14

            @SGaist Thank you for the hint. I will try that.

            J Offline
            J Offline
            JoeCFD
            wrote on 12 Dec 2022, 15:12 last edited by
            #4

            @dave2 void MyMainWindow::resizeEvent(QResizeEvent* event) may work better for you. changeEvent has more triggers. Does your code try to get full screen size and make some layout changes?

            D 1 Reply Last reply 12 Dec 2022, 20:28
            0
            • S SGaist
              11 Dec 2022, 21:05

              Hi,

              You can use the changeEvent and react on the event to get the size in the state of interest.

              D Offline
              D Offline
              dave2
              wrote on 12 Dec 2022, 20:25 last edited by
              #5

              @SGaist I was unable to get your suggestion to work. This is what I tried:

              void MyMainWindow::changeEvent(QEvent* event)
              {
                  if (event->type() == QEvent::WindowStateChange)
                  {
                      [logging code]
                  }
                  if (event->type() == QEvent::ActivationChange)
                  {
                      [logging code]
                  }
                  QMainWindow::changeEvent(event);
              }
              

              Running my code with the above MyMainWindow::changeEvent() (as well as with MyMainWindow::resizeEvent() and MyMainWindow::showEvent() from my original message) reveals that:

              1. The sequence of calls at start-up is:
                (1) changeEvent() with type = WindowStateChange
                (2) resizeEvent()
                (3) showEvent()
                (4) changeEvent() with type = ActivationChange
                (5) resizeEvent()
              2. The maximized size of MyMainWindow (and the corresponding sizes of the widgets contained in MyMainWindow, including the widget I am interested in) is not available until (5), i.e., not until the second call to resizeEvent().

              As I wrote in my original message, I could react to the 2nd call to MyMainWindow::resizeEvent(). And we are back to the questions I already raised:

              • How reliable would that method be?
              • Could it depend on the Qt version?
              • Would it also work on other platforms (Linux for example)?
              • Is there a better way to do this?
              1 Reply Last reply
              0
              • J JoeCFD
                12 Dec 2022, 15:12

                @dave2 void MyMainWindow::resizeEvent(QResizeEvent* event) may work better for you. changeEvent has more triggers. Does your code try to get full screen size and make some layout changes?

                D Offline
                D Offline
                dave2
                wrote on 12 Dec 2022, 20:28 last edited by
                #6

                @JoeCFD My code does not try to make layout changes, but some business changes. Those changes depend on the size of a certain widget inside MyMainWindow. For MyMainWindow::resizeEvent(QResizeEvent* event), see my original message, as well as my latest answer to @SGaist.

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hskoglund
                  wrote on 12 Dec 2022, 20:36 last edited by
                  #7

                  Hi, another approach is to delay those business decisions/changes until after MainWindow's constructor is done (and the QWidgets have all their resizing done) say via a lambda, include "qtimer.h" and

                  ...
                  ui->setupUi(this);
                  [other setup code]
                  showMaximized();
                  ...
                  QTimer::singleShot(20,this,[this]
                  {
                  // ... the rest of the startup code, i.e. business decisions
                  });
                  // end of MainWindow constructor
                  

                  20 ms is usually enough time to allow all the QWidgets to resize twice properly.

                  D 2 Replies Last reply 13 Dec 2022, 07:04
                  0
                  • H hskoglund
                    12 Dec 2022, 20:36

                    Hi, another approach is to delay those business decisions/changes until after MainWindow's constructor is done (and the QWidgets have all their resizing done) say via a lambda, include "qtimer.h" and

                    ...
                    ui->setupUi(this);
                    [other setup code]
                    showMaximized();
                    ...
                    QTimer::singleShot(20,this,[this]
                    {
                    // ... the rest of the startup code, i.e. business decisions
                    });
                    // end of MainWindow constructor
                    

                    20 ms is usually enough time to allow all the QWidgets to resize twice properly.

                    D Offline
                    D Offline
                    dave2
                    wrote on 13 Dec 2022, 07:04 last edited by
                    #8

                    @hskoglund Interesting idea, thanks. I'll give it a try.

                    1 Reply Last reply
                    0
                    • H hskoglund
                      12 Dec 2022, 20:36

                      Hi, another approach is to delay those business decisions/changes until after MainWindow's constructor is done (and the QWidgets have all their resizing done) say via a lambda, include "qtimer.h" and

                      ...
                      ui->setupUi(this);
                      [other setup code]
                      showMaximized();
                      ...
                      QTimer::singleShot(20,this,[this]
                      {
                      // ... the rest of the startup code, i.e. business decisions
                      });
                      // end of MainWindow constructor
                      

                      20 ms is usually enough time to allow all the QWidgets to resize twice properly.

                      D Offline
                      D Offline
                      dave2
                      wrote on 13 Dec 2022, 19:47 last edited by
                      #9

                      @hskoglund I confirm that this works nicely. Thanks!

                      1 Reply Last reply
                      0

                      1/9

                      11 Dec 2022, 20:31

                      • Login

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