Results 1 to 3 of 3

Thread: Question about QWidget::adjustSize()

  1. #1
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Question about QWidget::adjustSize()

    Hello All,

    I have a QFrame with several QLabels in it managed by QVBoxLayout. The QFrame itself is placed on top of QGraphicsView and its size is not managed by any layout. I want to be able to add/remove QLabels and adjust the size of the QFrame accordingly, and I've thought that adjustSize() is what I need. But after adding one more child QLabel and calling adjustSize() the size of the QFrame remains the same ! Moreover its sizeHint() and minimumSizeHint() remains the same too.

    What is the correct way to implement size adjusting when adding more child widgets ? Thanks in advance !

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Question about QWidget::adjustSize()

    Hello

    The layout will automatically adjust size of the container widget to be large enough to contain all added widgets. Are you calling QBoxLayout::addWidget() or QWidget::addWidget()? Notice that the former is not a reimplementation of the latter but an overloaded function.

    Actually you are only needed to call QWidget::adjustSize() if you want to make the container widget smaller after hiding its children. This step can also be made automatic by setting layout's size constraint to QLayout::SetFixedSize.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Vladimir (30th August 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question about QWidget::adjustSize()

    Thanks, after setting layout's size constraint to QLayout::SetFixedSize everything works as expected.

    But without it and when using adjustSize I'm getting strange sequence of sizes which you can see in the attached screenshots. The code which produces it:
    Qt Code:
    1. if(!_messagesFrame && _worldView) {
    2. _messagesFrame = new QFrame(_worldView);
    3. _messagesFrame->setFrameShape(QFrame::StyledPanel);
    4. _messagesFrame->setStyleSheet("border: 2px solid #8f8f91; border-radius: 6px; background-color: #e0e0e0; ");
    5. _messagesFrame->move(15,15);
    6. _messagesLayout = new QVBoxLayout(_messagesFrame);
    7. _messagesLayout->setContentsMargins(2,0,2,0);
    8. }
    9.  
    10. QWidget* widget = new QWidget(_messagesFrame);
    11. QLabel* label = new QLabel(text, widget);
    12.  
    13. QHBoxLayout* layout = new QHBoxLayout(widget);
    14. layout->setContentsMargins(0,0,0,0);
    15. layout->addWidget(label);
    16.  
    17. _messagesLayout->addWidget(widget);
    18. _messagesFrame->show();
    19. _messagesFrame->adjustSize();
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

Similar Threads

  1. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 18:49
  2. Replies: 1
    Last Post: 15th March 2007, 21:45
  3. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 16:34
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 15: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.