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. Get the MainWindow size
Forum Update on Monday, May 27th 2025

Get the MainWindow size

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.8k 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.
  • F Offline
    F Offline
    fem_dev
    wrote on last edited by
    #1

    I have a MainWindow with a graphicsView widget.

    I would like to match the sizes of the Main Window and the graphicsView widget.
    I'm trying this:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        setCentralWidget(ui->graphicsView);
    
        // Try to get the MainWindow size and set these values to the image size:
        int sizeX = this->size().width();
        int sizeY = this->size().height();
    
        // Create and init the image:
        QImage image = QImage(sizeX, sizeY, QImage::Format_RGB32);
    
        // Fill the image with colors:
        for (int x = 0; x < sizeX; x++) {
            for (int y = 0; y < sizeY; y++) {
                image.setPixel(x, y, qRgb(x * 256 / sizeX, y * 256 / sizeY, 200));
            }
        }
    
        // Create a graphics scene:
        QGraphicsScene* graphic = new QGraphicsScene(this);
        graphic->setSceneRect(0, 0, sizeX, sizeY);
    
        // Set the image:
        graphic->addPixmap(QPixmap::fromImage(image));
    
        // Try to fit in view:
        ui->graphicsView->fitInView(0, 0, sizeX, sizeY);
    
        // Display it on the UI:
        ui->graphicsView->setScene(graphic);
    }
    

    But the result is this window with scroll bars!!

    6871a55b-5275-4549-9272-8c4f6969b5b4-image.png

    How can I init the MainWindow and the graphicsView widget with the same size?

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

      Hi,

      As suggested in the fitInView documentation, it's usual to call it in a resizeEvent re-implementation.

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

      F 1 Reply Last reply
      1
      • F Offline
        F Offline
        fem_dev
        wrote on last edited by
        #2

        For now, I'm resolving this problem with this temporary source-code:

        // Get the correct MainWindow size to use with graphicsView widget:
        int width = this->size().width() - 20; // Magic number, but works
        int height = this->size().height() - 40; // Magic number, but works
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Try to get the size when the widget is shown, not when you create it - then there are no valid size information available iirc.

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

          F 1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            As suggested in the fitInView documentation, it's usual to call it in a resizeEvent re-implementation.

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

            F 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              Try to get the size when the widget is shown, not when you create it - then there are no valid size information available iirc.

              F Offline
              F Offline
              fem_dev
              wrote on last edited by
              #5

              @Christian-Ehrlicher thank you...
              I understand your point, but now I have a logistic doubt.

              I have only one QMainWindow and, inside of it, I have these two things: A QImage and a GraphicsView.
              So, I need to set:
              -> The QImage size
              -> The graphicsView size

              When the application starts, the QMainWindow appears and these values must be valids to create a good UI.

              How could I set these values before the QMainWindow initialization?

              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                As suggested in the fitInView documentation, it's usual to call it in a resizeEvent re-implementation.

                F Offline
                F Offline
                fem_dev
                wrote on last edited by
                #6

                @SGaist Yes, I'm using it in both cases:
                1- On the QMainWindow constructor
                2- On the resizeEvent()

                It solves the problem!
                Thank you again!

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

                  You're welcome !

                  Since you have it working now please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :-)

                  Note that you can also mark an answer as the "thread correct answer" by using the three vertical dots on the right of each answer.

                  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

                  • Login

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