Results 1 to 6 of 6

Thread: Widgets taking too much space hidden on a Layout

  1. #1

    Smile Widgets taking too much space hidden on a Layout

    So, I have a QDockWidget with 10 widgets inside... but using code, the first thing I do when I start my app is to hide all but one. Still, can't figure out a way to make my dock small. It always takes at minimum the size of all widgets being shown (although they are hidden).

    My question(s) therefore is:
    - Is remove/add better than hide/show for layout size managing?
    - Does the size calculated automatically by the QDesigner (visible inside the .ui file) changes anything about layout size?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Widgets taking too much space hidden on a Layout

    Which layout are you using? When are you hiding the widgets?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: Widgets taking too much space hidden on a Layout

    Thanks for the interest.

    The dock panel has a vertical layout holding 10 widgets. All this widgets are basically horizontal layouts with buttons.
    I'm hiding the inside widgets on the window constructor (widget1->hide()).

    The idea is that the panel has some buttons (one widget) showing for each state of my application, but the dock should always have the height of a line of buttons, never the height of 10 lines, like is currently taking.

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

    Default Re: Widgets taking too much space hidden on a Layout

    I do not have the explanation for the behaviour you are experiencing, but I have a suggestion. Apparently exactly one widget among the 10 must be visible at any given time. Instead of showing and hiding the widgets manually, I suggest you put them all in a QStackedLayout.

  5. #5

    Default Re: Widgets taking too much space hidden on a Layout

    Well, I simplified the problem by not saying that there's a state where I'm actually showing 2 at a time. That shouldn't make a difference on the size because when the layout is first created, all widgets are hidden but one, and the height is still big... this should make the qstackedlayout a non-option :/

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Widgets taking too much space hidden on a Layout

    Use QLayout::setSizeConstraint() to force the layout size to follow the size hint of visible rows of buttons.
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Widget: public QWidget {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *p = 0): QWidget(p) {
    8.  
    9. QVBoxLayout *vlayout = new QVBoxLayout;
    10. setLayout(vlayout);
    11. vlayout->setSizeConstraint(QLayout::SetFixedSize);
    12.  
    13. for (int r = 0; r < 3; ++r) {
    14. // Construct a new row
    15. QWidget *row = new QWidget(this);
    16. QHBoxLayout *hlayout = new QHBoxLayout;
    17. row->setLayout(hlayout);
    18. for (int c = 0; c < 5; ++c) {
    19. QPushButton *p = new QPushButton(QString("R%1C%2").arg(r).arg(c), row);
    20. hlayout->addWidget(p);
    21. }
    22. vlayout->addWidget(row);
    23. rows << row;
    24. }
    25.  
    26. // Hide all but first row
    27. for (int r = 1; r < rows.count(); ++r)
    28. rows.at(r)->hide();
    29.  
    30. // Trigger some changes in visibility
    31. QTimer::singleShot(3000, this, SLOT(showSecond()));
    32. QTimer::singleShot(8000, this, SLOT(hideSecond()));
    33. }
    34. public slots:
    35. void showSecond() { rows.at(1)->show(); }
    36. void hideSecond() {
    37. rows.at(0)->hide();
    38. rows.at(1)->hide();
    39. rows.at(2)->show();
    40. }
    41.  
    42. private:
    43. QWidgetList rows;
    44. };
    45.  
    46.  
    47. int main(int argc, char *argv[])
    48. {
    49. QApplication app(argc, argv);
    50.  
    51.  
    52. Widget w;
    53. w.show();
    54. return app.exec();
    55. }
    56. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Try it with and without line 11. It does constrain both directions though.
    Last edited by ChrisW67; 2nd September 2011 at 01:33.

Similar Threads

  1. QSplitter taking parenthood over its widgets
    By mak_user in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2011, 22:40
  2. Hiding Layout item - Layout does not use available space
    By Asperamanca in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2011, 10:51
  3. The delegated widgets become hidden
    By fulbay in forum Qt Programming
    Replies: 4
    Last Post: 20th December 2010, 13:53
  4. Qt layout space + stretch
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 20th December 2007, 16:53
  5. widgets behind hidden widgets not working
    By bpetty in forum Newbie
    Replies: 13
    Last Post: 7th September 2007, 21:23

Tags for this Thread

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.