Results 1 to 5 of 5

Thread: QSplitter, sizes() is empty (0,0) but contains widgets

  1. #1
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QSplitter, sizes() is empty (0,0) but contains widgets

    Hi,

    i have the following code in my constructor:

    Qt Code:
    1. QGridLayout *layout = new QGridLayout( parent );
    2.  
    3. pSplitter = new QSplitter();
    4. pSplitter->setOrientation( Qt::Vertical );
    5. pSplitter->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
    6. pSplitter->setHandleWidth( 10 );
    7.  
    8. pSplitter->addWidget( pressure );
    9. pSplitter->addWidget( temperature );
    10.  
    11. layout->addWidget( pSplitter, 0,0 );
    12.  
    13. qDebug() << pressure->height();
    14. qDebug() << temperature->height();
    15. qDebug() << pSplitter->sizes();
    To copy to clipboard, switch view to plain text mode 

    The debug output is:

    Qt Code:
    1. 200
    2. 200
    3. (0, 0)
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. (330, 330)
    2. (331, 329)
    3. (331, 329)
    4. (331, 329)
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSplitter, sizes() is empty (0,0) but contains widgets

    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.

  3. #3
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSplitter, sizes() is empty (0,0) but contains widgets

    I checked size of widget pressure and widget temperature in constructor

    Qt Code:
    1. qDebug() << pressure->size();
    2. qDebug() << temperature->size();
    To copy to clipboard, switch view to plain text mode 

    Result:

    Qt Code:
    1. QSize(200, 200)
    2. QSize(200, 200)
    To copy to clipboard, switch view to plain text mode 

    They are both visible and have a size.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSplitter, sizes() is empty (0,0) but contains widgets

    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSplitter, sizes() is empty (0,0) but contains widgets

    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?).

Similar Threads

  1. Arrange Widgets in QSplitter
    By vinodpaul in forum Newbie
    Replies: 3
    Last Post: 7th September 2012, 10:17
  2. How to remove widgets from QSplitter?
    By mcb in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 06:10
  3. QSplitter::saveState returns empty string
    By miraks in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2008, 21:30
  4. How to set Widgets sizes with in QSplitter
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 07:38
  5. Remove widgets from a QSplitter
    By blukske in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2006, 10:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.