Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. how to get the current height and width of the window?
Forum Updated to NodeBB v4.3 + New Features

how to get the current height and width of the window?

Scheduled Pinned Locked Moved Unsolved Qt 6
5 Posts 3 Posters 1.3k 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
    aria_aa
    wrote on last edited by aria_aa
    #1

    Hi everyone.
    I want to find the height and width of the window in Qt6 but it seems something is wrong with Qt or with my knowledge of it!! (probably the second one is correct 😜)

    the size after maximize is still the same as the size that I set but after minimizing it increase weirdly.
    did I do anything wrong?

    here is my code:

    #include <QApplication>
    #include <QtWidgets>
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[]){
        QApplication app (argc, argv);
    
        QWidget win;
        win.setWindowTitle("Main Window");
        win.move(400, 200);
        win.resize(500, 300);
        win.show();
    
        cout << "Window size is :" << endl;
        int win_cw = win.width();
        int win_ch = win.height();
        cout << "width : " << win_cw << "\nheight : " << win_ch << endl;
    
        cout << "Window size after maximized is:" << endl;
        win.showMaximized();
        win_cw = win.width();
        win_ch = win.height();
        cout << "width : " << win_cw << "\nheight : " << win_ch << endl;
    
        cout << "Window size after minimized is:" << endl;
        win.showMinimized();
        win_cw = win.width();
        win_ch = win.height();
        cout << "width : " << win_cw << "\nheight : " << win_ch << endl;
    
        return app.exec();
    }
    

    and here is the output of the code:

    Window size is :
    width : 500
    height : 300
    Window size after maximized is:
    width : 500
    height : 300
    Window size after minimized is:
    width : 1536
    height : 810
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can't get a valid size until the window is shown. show() just tells the window manager to show the window. --> You have to ask for the size later (e.g. in a slot)

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

      A 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        You can't get a valid size until the window is shown. show() just tells the window manager to show the window. --> You have to ask for the size later (e.g. in a slot)

        A Offline
        A Offline
        aria_aa
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in how to get the current height and width of the window?:

        You can't get a valid size until the window is shown. show() just tells the window manager to show the window. --> You have to ask for the size later (e.g. in a slot)

        thank you for your answer, but I'm a little confused.
        do you know any documents that I must read to know more about it?
        what I should do in this code to get the correct size?

        Christian EhrlicherC JonBJ 2 Replies Last reply
        0
        • A aria_aa

          @Christian-Ehrlicher said in how to get the current height and width of the window?:

          You can't get a valid size until the window is shown. show() just tells the window manager to show the window. --> You have to ask for the size later (e.g. in a slot)

          thank you for your answer, but I'm a little confused.
          do you know any documents that I must read to know more about it?
          what I should do in this code to get the correct size?

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

          @aria_aa said in how to get the current height and width of the window?:

          what I should do in this code to get the correct size?

          You have to ask for the size later (e.g. in a slot)

          This also answers your question here: https://forum.qt.io/topic/133979/how-to-make-a-window-in-the-middle-of-the-screen

          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
          • A aria_aa

            @Christian-Ehrlicher said in how to get the current height and width of the window?:

            You can't get a valid size until the window is shown. show() just tells the window manager to show the window. --> You have to ask for the size later (e.g. in a slot)

            thank you for your answer, but I'm a little confused.
            do you know any documents that I must read to know more about it?
            what I should do in this code to get the correct size?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @aria_aa
            One way is to subclass your QWidget and override void QWidget::showEvent(QShowEvent *event), and read the size there.

            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