Results 1 to 4 of 4

Thread: Expanding and hiding a widget

  1. #1
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Expanding and hiding a widget

    First I want to thank you for your time, I’m new to Qt and I hope I won’t bother you but I have a simple question and I hope I can get a simple answer.

    The circumstances are the following:
    I made a QmainWindow
    That has 1+3 widgets:

    // a main widget
    QWidget *centralWidget;
    //with a main Layout
    QGridLayout *mainLayout;

    //a top Widget (child of centralWidget)
    QWidget *topWidget;
    //with a top Layout
    QGridLayout *topLayout;
    //[…]more childWidgets of topWidget,
    //including a moreButton that when I press it expands middle Widget

    //a middle Widget (child of centralWidget)
    QWidget *middleWidget;
    //with a middle Layout
    QVBoxLayout *middleLayout;
    //[…]more childWidgets of middleWidget;

    //a bottom Widget (child of centralWidget)
    QWidget * bottom Widget;
    //with a bottom Layout
    QHBoxLayout * bottom Layout;
    //[…]more childWidgets of bottom Widget;

    So I hide the middleWidget->hide() in the constructor;

    Then I resize the window so it will start “compressed”
    Qt Code:
    1. QSize size(0, 0);
    2. size = size.expandedTo(mainLayout->minimumSize());
    3. resize(size);
    To copy to clipboard, switch view to plain text mode 

    I connect the more button
    Qt Code:
    1. connect(moreButton, SIGNAL(toggled(bool)),
    2. middleWidget, SLOT(setVisible(bool)));
    To copy to clipboard, switch view to plain text mode 

    and it works well but the problem is that after I made it visible and I hide again the middleWidget the window doesn’t compress back, the MainLayout just covers the empty space left by the middleWidget.

    I even made a SLOT makeSmall that I connected to the moreButton so that it will
    compress again but id doesn't change the size.

    Qt Code:
    1. QSize size(0, 0);
    2. size = size.expandedTo(mainLayout->minimumSize());
    3. this->resize(size);
    To copy to clipboard, switch view to plain text mode 

    If I make the middleLayout visible again the window will keep the same size.

    I want to make the window shrink when I hide the widget but I don't know how how to do it every time I hide the widget (as I sad it compresses at start but afterwards it remains big).

    Sorry if the post is a bit confusing.
    Last edited by Dauntless; 11th December 2006 at 17:37.

  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: Expanding and hiding a widget

    Use QLayout::setSizeConstraint(). If in doubt, search the forum for the method name, there has recently been a thread about the same problem.

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

    Dauntless (12th December 2006)

  4. #3
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Expanding and hiding a widget

    Thank you, I search and found this thread regarding the same issue. But I still don't know why the window doesn't shrink back .

    I made this little project, maybe you can tell me what I forget to do:

    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "testclass.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. TestClass w;
    8. w.show();
    9. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    testclass.h
    Qt Code:
    1. #ifndef TESTCLASS_H
    2. #define TESTCLASS_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QtGui/QWidget>
    6. #include <QtGui/QVBoxLayout>
    7. #include <QtGui/QHBoxLayout>
    8. #include <QtGui/QLabel>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QCheckBox>
    11.  
    12. class TestClass : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. TestClass(QWidget *parent = 0, Qt::WFlags flags = 0);
    18. ~TestClass();
    19.  
    20. private:
    21. QWidget *mainWidget;
    22. QVBoxLayout *mainLayout;
    23.  
    24. QWidget *topWidget;
    25. QHBoxLayout *topLayout;
    26. QLabel *topLabel;
    27. QPushButton *moreButton;
    28.  
    29. QWidget *middleWidget;
    30. QVBoxLayout *middleLayout;
    31. QLabel *middleLabel;
    32. QCheckBox *checkBox;
    33.  
    34. QWidget *bottomWidget;
    35. QVBoxLayout *bottomLayout;
    36. QLabel *bottomLabel;
    37. };
    38.  
    39. #endif // TESTCLASS_H
    To copy to clipboard, switch view to plain text mode 

    testclass.cpp
    Qt Code:
    1. #include "testclass.h"
    2.  
    3. TestClass::TestClass(QWidget *parent, Qt::WFlags flags)
    4. : QMainWindow(parent, flags)
    5. {
    6. mainWidget = new QWidget(this);
    7. mainLayout = new QVBoxLayout(mainWidget);
    8.  
    9. //topWidget
    10.  
    11. topWidget = new QWidget(mainWidget);
    12. topLayout = new QHBoxLayout(topWidget);
    13.  
    14. topLabel = new QLabel(topWidget);
    15. topLabel->setText("This is the top widget");
    16. moreButton = new QPushButton(topWidget);
    17. moreButton->setText("Press to see the middle widget");
    18. moreButton->setCheckable(true);
    19.  
    20. topLayout->setSizeConstraint(QLayout::SetFixedSize);
    21. topLayout->addWidget(topLabel);
    22. topLayout->addWidget(moreButton);
    23.  
    24. //middleWidget
    25.  
    26. middleWidget = new QWidget(mainWidget);
    27. middleLayout = new QVBoxLayout(middleWidget);
    28.  
    29. middleLabel = new QLabel(middleWidget);
    30. middleLabel->setText("This is the middle widget");
    31. checkBox = new QCheckBox(middleWidget);
    32. checkBox->setText("Extra Option");
    33.  
    34. middleLayout->setSizeConstraint(QLayout::SetFixedSize);
    35. middleLayout->addWidget(middleLabel);
    36. middleLayout->addWidget(checkBox);
    37.  
    38. //bottomWidget
    39.  
    40. bottomWidget = new QWidget(mainWidget);
    41. bottomLayout = new QVBoxLayout(bottomWidget);
    42.  
    43. bottomLabel = new QLabel(bottomWidget);
    44. bottomLabel->setText("This is the bottom widget");
    45.  
    46. bottomLayout->setSizeConstraint(QLayout::SetFixedSize);
    47. bottomLayout->addWidget(bottomLabel);
    48.  
    49. //mainLayout
    50.  
    51. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
    52. mainLayout->addWidget(topWidget);
    53. mainLayout->addWidget(middleWidget);
    54. mainLayout->addWidget(bottomWidget);
    55.  
    56. middleWidget->hide();
    57.  
    58. connect(moreButton, SIGNAL(toggled(bool)),
    59. middleWidget, SLOT(setVisible(bool)));
    60.  
    61. QSize size(0, 0);
    62. size = size.expandedTo(mainWidget->minimumSize());
    63. resize(size);
    64.  
    65. setCentralWidget(mainWidget);
    66. setWindowTitle("Testing middleWidget Expansion");
    67.  
    68. }
    69.  
    70. TestClass::~TestClass()
    71. {
    72.  
    73. }
    To copy to clipboard, switch view to plain text mode 

    The window size doesn't shrink back when I hide again the middleWidget.

    I even tried to add a public slot:
    Qt Code:
    1. void TestClass::makeSmall(bool r)
    2. {
    3. middleWidget->setVisible(r);
    4.  
    5. QSize size(0, 0);
    6. size = size.expandedTo(mainWidget->minimumSize());
    7. resize(size);
    8. }
    To copy to clipboard, switch view to plain text mode 

    and use
    Qt Code:
    1. connect(moreButton, SIGNAL(toggled(bool)),
    2. this, SLOT(makeSmall(bool)));
    To copy to clipboard, switch view to plain text mode 
    insead but it doesn't work.

    Thank you for your time.

  5. #4
    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: Expanding and hiding a widget

    The widget has to have the default size set to the size without the additional widgets. You should reimplement sizeHint() or do something simmilar.

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.