PDA

View Full Version : QSplitter, sizes() is empty (0,0) but contains widgets



HappyCoder
23rd July 2015, 14:19
Hi,

i have the following code in my constructor:



QGridLayout *layout = new QGridLayout( parent );

pSplitter = new QSplitter();
pSplitter->setOrientation( Qt::Vertical );
pSplitter->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
pSplitter->setHandleWidth( 10 );

pSplitter->addWidget( pressure );
pSplitter->addWidget( temperature );

layout->addWidget( pSplitter, 0,0 );

qDebug() << pressure->height();
qDebug() << temperature->height();
qDebug() << pSplitter->sizes();


The debug output is:



200
200
(0, 0)


I have a slot to follow the splitterMoved(int,int) signal, there i got this output
for sizes() after splitter movement.



(330, 330)
(331, 329)
(331, 329)
(331, 329)


Why is sizes() == (0,0) in constructor and contain values after "splitterMoved" singal.
Do i miss something in my code or is sizes() only updated when the splitter was moved?

Thx

yeye_olive
23rd July 2015, 14:36
From the documentation of QSplitter::sizes():

Note that invisible widgets have a size of 0.
The widgets are probably still invisible when you create the QSplitter. They will only get a size after they (or one of their parent, typically the top-level window) is shown.

HappyCoder
23rd July 2015, 14:47
I checked size of widget pressure and widget temperature in constructor



qDebug() << pressure->size();
qDebug() << temperature->size();


Result:



QSize(200, 200)
QSize(200, 200)


They are both visible and have a size.

yeye_olive
23rd July 2015, 15:06
Which constructor are you talking about? How do you know that the widgets are visible at that point?

My understanding of the documentation is that QSplitter::sizes() returns zero values for the invisible sub-widgets, even if their own QWidget::size() returns a non-zero value. This may not be intuitive to you, but what you observe seems aligned with the documentation.

d_stranz
23rd July 2015, 16:56
What yeye_olive is trying to explain is that when you are in the constructor, your widgets might be created, but they are not visible because the showEvent() for the widget instance you are constructing has not been called yet. So all these new widget instances are invisible and therefore could have zero size. If you implement showEvent() for your widget and put your qDebug statement in that, you will see that your splitter and all the widgets inside it now have proper sizes.

You should also be aware that resizeEvent() can occur before showEvent(), and your widgets may not have correct sizes there either. The reason your widgets have proper sizes in your splitterMoved() slot is because the showEvent() has already occurred at that point (or else you wouldn't have splitter handles to move, right?).