Wrong widget layout after maximized show
-
Hello everyone !
I was doing some tests with a dummy Qt C++ desktop app (VS 2017 x64 Qt 5.12 & 5.9 Win 10)
And I noticed a weird behaviour. I checked around on stackoverflow and here but I didn't find any answer yet.Here is the issue:
I open a top level widget and maximize its size. (just a QPushButton)
Then I close it by pressing the button which is connected to the hide slot.
And then reopen the widget with another button calling its show slot and hope to see the same thing.But I end up with this wrong painting / layout with the button blocked in top left corner of the window.
Any idea on the origin of this behaviour ? What am I doing wrong ? I spent some time in the doc and even started scratching the top of Qt source but nothing weird so far.
Thank you in advance for any help !
And happy new year by the way ^^ !
Cheers,NB: I feel it is connected with the fact that widgets don't remember their size when we hide and show them again. They just remember if they are maximized or not and their position in the latter case.
NB2: Source code
---------------------main.cpp
#include "testapqtgui.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
testapqtgui w;
w.show();
return a.exec();
}--------------testapqtgui.h
#pragma once
#include <QtWidgets/QMainWindow>
class testapqtgui : public QMainWindow
{
Q_OBJECTpublic:
testapqtgui(QWidget *parent = Q_NULLPTR);private:
};------------------testapqtgui.cpp
#include "testapqtgui.h"
#include <QPushButton>
testapqtgui::testapqtgui(QWidget *parent)
: QMainWindow(parent)
{
QPushButton *const button = new QPushButton(tr("Click me !"));
setCentralWidget(button);QPushButton *popupWidget = new QPushButton(tr("Close")); connect(button, &QPushButton::clicked, popupWidget, &QWidget::show); connect(popupWidget, &QPushButton::clicked, popupWidget, &QWidget::hide);
}
-
Hi @LeLev !
Oh wow nice finding ! Thanks for the information. I'll go with this workaround.
I will dig in the source of Qt to see if there is not an improvement to be made here.I will leave the topic open a little bit more to see if someone has a complete answer, otherwise I will close it in 24-48 hrs as resolved.
Thanks again !
EDIT: I found something new. You don't even need to resize the window. You just need to set the resized flag to solve the issue.
popupWidget->setAttribute(Qt::WA_Resized);EDIT2: Ok now I think I have the proper final solution for my use case:
popupWidget->adjustSize();It will compute the right size, do a resize therefore set the flag. So you have no visual issue and you have a proper initial size.
Thank you very much @LeLev ! You gave me the initial bit of information I needed to fix my issue :D ! Have a great day !
-
Hi @LeLev !
Oh wow nice finding ! Thanks for the information. I'll go with this workaround.
I will dig in the source of Qt to see if there is not an improvement to be made here.I will leave the topic open a little bit more to see if someone has a complete answer, otherwise I will close it in 24-48 hrs as resolved.
Thanks again !
EDIT: I found something new. You don't even need to resize the window. You just need to set the resized flag to solve the issue.
popupWidget->setAttribute(Qt::WA_Resized);EDIT2: Ok now I think I have the proper final solution for my use case:
popupWidget->adjustSize();It will compute the right size, do a resize therefore set the flag. So you have no visual issue and you have a proper initial size.
Thank you very much @LeLev ! You gave me the initial bit of information I needed to fix my issue :D ! Have a great day !
-
I have the same problem with my project...
In my opinion they mixed up "Maximize" and "Minimize".
If you put one PushButton and something different like a Label inside a layout box and set the PushButtons size policy to max, the LABEL size increases while Pushbutton itself gets smaller and vice versa.
If you use min, the objects have the same weird behavior...
I really think they mixed them up.