Getting the size of the MainWindow
-
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 -
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.
-
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. -
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.
-
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.@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,
resizeEventis 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 ?
-
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.)
-
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@JYG61 simple put your QLabel inside a qscrollArea, then set the ScrollArea as central widget. No need to worry about the windows size
-
@hskoglund
Using a timer can be anything between a waste of time and vain hope.
It depends on the operating system, howshowMaximized(),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)returnstrue.
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()andshowFullScreen().
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.