PDA

View Full Version : Usage of QLayout overrides size of resized widget



danny-g
10th October 2012, 16:26
In my program I open a widget and I put a label in it. I want the label to appear in the middle of the widget, so I use a layout. However, when I resize the window, either programmatically with setGeometry or with the mouse, the widget is first resized to the requested size, and then there is an additional size modification. I receive multiple resize events. I tried several QLayout settings, but I haven't find a way to overcome this behavior.
The code of creating the widget is as follows:


renderWin = new QWidget() ;
renderWin->installEventFilter(this) ;
QLabel *powered = new QLabel ;
powered->setPixmap(QPixmap(":/images/poweredbyq2.png")) ;
powered->setMaximumSize(512, 512) ;
powered->setScaledContents(true) ;
QHBoxLayout *hl = new QHBoxLayout(renderWin) ;
hl->addSpacerItem(new QSpacerItem(5, 5)) ;
hl->addWidget(powered) ;
hl->addSpacerItem(new QSpacerItem(5, 5)) ;
hl->setStretch(0, 1) ;
// hl->setStretch(1, 1) ;
hl->setStretch(2, 1) ;
hl->setContentsMargins(0, 0, 0, 0) ;
hl->setSpacing(0) ;


How can I make the layout be resized according to the widget size? It is not a problem of minimal and maximal size, because the new size is always modified, no matter what the new dimensions are.

I will appreciate any advise

ChrisW67
11th October 2012, 00:14
Layouts size the widgets within the constraints supplied, not the other way around. Why do you care that your receive multiple resize events (which is normal as you drag) and what does that have to do with keeping the label horizontally centred?

danny-g
11th October 2012, 08:31
The important thing for me is to open the window with a certain size. I don't use setFixedSize because it needs to be resizable. The way I wrote it, the window is opened with the desired dimensions, and then its size changes. There are no explicit constraints, so I don't understand why the window size has to change. I don't care about multiple events during drag. I am concerned that the window resizing does not exactly follow the mouse movement. That happens because after getting the resize request, the layout "decides" on a slightly different size.

wysota
11th October 2012, 08:59
Maybe the size you set is not enough to accomodate all widgets inside?

danny-g
11th October 2012, 09:17
There are no additional widgets inside, other than the label and the two spacer. Also, I can drag the window to be even smaller. Could it be that some property forces a certain height to width ratio?

wysota
11th October 2012, 10:00
Please prepare a minimal compilable example reproducing the problem.

danny-g
14th October 2012, 15:06
OK, while trying to prepare the 'minimal compilable example, I figure out what was wrong with my window. It has nothing to do with the layout, although I noticed the problem only after adding a layout. The problem, in short, was incorrect synchronization of two rendering systems (Qt and a 3D modeling software that uses the same screen space). It works fine now, and the QLayout operates as expected.