Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Getting the size of the MainWindow
Qt 6.11 is out! See what's new in the release blog

Getting the size of the MainWindow

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
9 Posts 6 Posters 376 Views 2 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.
  • J Offline
    J Offline
    JYG61
    wrote last edited by
    #1

    Hello,
    I want to display an image on the MainWindow of my C++ program under QT, the following code that is in the MainWindow creator maximize the Window and then try to get his size to resize the QImage and the QLabel but the size that i get is the size before it is maximized, i used the following code :
    ui->setupUi (this);
    this->showMaximized ();
    szx=this->size ().width ();
    szy=this->size ().height ();
    Can you tell me what is wrong in this ?
    Thanks,
    JY Giroud

    Ronel_qtmasterR 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      The showXYZ methods requests the widget to be shown. This won't have happened yet when you call this->size().

      The showEvent method is were you will get the size of your widget once it's shown. Depending on what you want to do, that might be enough.

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

      1 Reply Last reply
      2
      • J Offline
        J Offline
        JYG61
        wrote last edited by
        #3

        I am not sure i understand well : showEvent () is a method of QMainWindow that run every this the MainWindow is writen, it will run when i call the showMaximized function, i just need to read the size in that method ?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JYG61
          wrote last edited by
          #4

          I added that function
          void MainWindow::showEvent(QShowEvent *event)
          {
          int szx,szy;
          szx=this->size ().width ();
          szy=this->size ().height ();
          }
          when i put a breakpoint in the line szy=... the value of szx is 800 (the size before the window is maximized), i expected i passed twice in that function :
          the first display with the standard size
          the second after the maximize function
          but it stops only once
          after the first stop i pressed F10 to execute one line and then F5 to stop at the next breakpoint but it doesn't stop anymore, maybe i don't understand the way the debugger works.

          SGaistS 1 Reply Last reply
          0
          • Jacek Marcin JaworskiJ Offline
            Jacek Marcin JaworskiJ Offline
            Jacek Marcin Jaworski
            wrote last edited by Jacek Marcin Jaworski
            #5

            You make mistake. You need to use QMainWindow::resizeEvent() in order to receive info that window was resized. Then you can call QMainWindow::update() in order to repaint whole window. QMainWindow::showEvent() indicating only that the window appear on the screen to your user, not that the window is updated/resized/repainted.

            Jacek Marcin Jaworski, Pruszcz Gd., Pomeranian Voivodeship, POLAND🇵🇱, EU 🇪🇺;
            WWW home page (in Polish): <https://energokod.gda.pl>;
            email: <jmj@energokod.gda.pl>;
            tel.: +48-609-170-742, best at: 5:00-5:55 or 16:00-17:25.

            1 Reply Last reply
            0
            • J JYG61

              I added that function
              void MainWindow::showEvent(QShowEvent *event)
              {
              int szx,szy;
              szx=this->size ().width ();
              szy=this->size ().height ();
              }
              when i put a breakpoint in the line szy=... the value of szx is 800 (the size before the window is maximized), i expected i passed twice in that function :
              the first display with the standard size
              the second after the maximize function
              but it stops only once
              after the first stop i pressed F10 to execute one line and then F5 to stop at the next breakpoint but it doesn't stop anymore, maybe i don't understand the way the debugger works.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote last edited by
              #6

              @JYG61 Going through it twice is not what I would have expected. Which OS are you on ?

              That said, based on your requirements, @Jacek-Marcin-Jaworski is correct, resizeEvent is called on each operation that modifies the size of your widget.

              By the way, how are you using your QLabel ?

              Did you consider using QLabel::scaledContents property ?

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

              1 Reply Last reply
              0
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote last edited by
                #7

                Hi, to add to @SGaist instead of listening to the resizeEvent() you could split the code in your MainWindow creator code into 2 parts using a singleshot lambda, something like this:

                ...
                ui->setupUi(this);
                [other setup code]
                showMaximized();
                ...
                
                QTimer::singleShot(20,this,[this]
                {
                //  get the size of your (now maximized) main window here
                });
                // end of MainWindow constructor
                

                (20 millseconds should be enough for the (usually two) resize events to happen.)

                1 Reply Last reply
                0
                • J JYG61

                  Hello,
                  I want to display an image on the MainWindow of my C++ program under QT, the following code that is in the MainWindow creator maximize the Window and then try to get his size to resize the QImage and the QLabel but the size that i get is the size before it is maximized, i used the following code :
                  ui->setupUi (this);
                  this->showMaximized ();
                  szx=this->size ().width ();
                  szy=this->size ().height ();
                  Can you tell me what is wrong in this ?
                  Thanks,
                  JY Giroud

                  Ronel_qtmasterR Offline
                  Ronel_qtmasterR Offline
                  Ronel_qtmaster
                  wrote last edited by Ronel_qtmaster
                  #8

                  @JYG61 simple put your QLabel inside a qscrollArea, then set the ScrollArea as central widget. No need to worry about the windows size

                  Check this https://youtu.be/lps8oayHPxs?si=C5kmvyT_W8SOXF4p

                  1 Reply Last reply
                  0
                  • Axel SpoerlA Offline
                    Axel SpoerlA Offline
                    Axel Spoerl
                    Moderators
                    wrote last edited by
                    #9

                    @hskoglund
                    Using a timer can be anything between a waste of time and vain hope.
                    It depends on the operating system, how showMaximized(), showNormal() and the like are handled.
                    QWidget::windowState() reflects what has been ordered to the window manager.

                    The problem is that the widget's geometry can change twice. First time when showMaximized() has been processed and
                    widget->windowState().testFlag(Qt::WindowMaximized) returns true.
                    Second time, when the window manager has applied decorations (i.e. title bar and window handles).

                    A good place to look for inspiration is tst_QWidget::showMaximized() and showFullScreen().
                    The QTRY_ macros generally process events until either the predicate is true or a timeout is hit. They're not available in applications, but you can write your own methods that work for your use case.

                    Software Engineer
                    The Qt Company, Oslo

                    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