how to get the current height and width of the window?
-
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
-
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)
-
@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? -
@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