PDA

View Full Version : Expanding and hiding a widget



Dauntless
11th December 2006, 18:29
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”


QSize size(0, 0);
size = size.expandedTo(mainLayout->minimumSize());
resize(size);

I connect the more button


connect(moreButton, SIGNAL(toggled(bool)),
middleWidget, SLOT(setVisible(bool)));

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.



QSize size(0, 0);
size = size.expandedTo(mainLayout->minimumSize());
this->resize(size);

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.

wysota
11th December 2006, 20:38
Use QLayout::setSizeConstraint(). If in doubt, search the forum for the method name, there has recently been a thread about the same problem.

Dauntless
12th December 2006, 07:11
Thank you, I search and found this (http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-keep-widgets-size-to-one-which-is-just-sufficiennt-to-display-contents-4788.html/?highlight=setSizeConstraint) thread regarding the same issue. But I still don't know why the window doesn't shrink back :o .

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

main.cpp:


#include <QtGui/QApplication>
#include "testclass.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestClass w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}


testclass.h


#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QtGui/QMainWindow>
#include <QtGui/QWidget>
#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QCheckBox>

class TestClass : public QMainWindow
{
Q_OBJECT

public:
TestClass(QWidget *parent = 0, Qt::WFlags flags = 0);
~TestClass();

private:
QWidget *mainWidget;
QVBoxLayout *mainLayout;

QWidget *topWidget;
QHBoxLayout *topLayout;
QLabel *topLabel;
QPushButton *moreButton;

QWidget *middleWidget;
QVBoxLayout *middleLayout;
QLabel *middleLabel;
QCheckBox *checkBox;

QWidget *bottomWidget;
QVBoxLayout *bottomLayout;
QLabel *bottomLabel;
};

#endif // TESTCLASS_H


testclass.cpp


#include "testclass.h"

TestClass::TestClass(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
mainWidget = new QWidget(this);
mainLayout = new QVBoxLayout(mainWidget);

//topWidget

topWidget = new QWidget(mainWidget);
topLayout = new QHBoxLayout(topWidget);

topLabel = new QLabel(topWidget);
topLabel->setText("This is the top widget");
moreButton = new QPushButton(topWidget);
moreButton->setText("Press to see the middle widget");
moreButton->setCheckable(true);

topLayout->setSizeConstraint(QLayout::SetFixedSize);
topLayout->addWidget(topLabel);
topLayout->addWidget(moreButton);

//middleWidget

middleWidget = new QWidget(mainWidget);
middleLayout = new QVBoxLayout(middleWidget);

middleLabel = new QLabel(middleWidget);
middleLabel->setText("This is the middle widget");
checkBox = new QCheckBox(middleWidget);
checkBox->setText("Extra Option");

middleLayout->setSizeConstraint(QLayout::SetFixedSize);
middleLayout->addWidget(middleLabel);
middleLayout->addWidget(checkBox);

//bottomWidget

bottomWidget = new QWidget(mainWidget);
bottomLayout = new QVBoxLayout(bottomWidget);

bottomLabel = new QLabel(bottomWidget);
bottomLabel->setText("This is the bottom widget");

bottomLayout->setSizeConstraint(QLayout::SetFixedSize);
bottomLayout->addWidget(bottomLabel);

//mainLayout

mainLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addWidget(topWidget);
mainLayout->addWidget(middleWidget);
mainLayout->addWidget(bottomWidget);

middleWidget->hide();

connect(moreButton, SIGNAL(toggled(bool)),
middleWidget, SLOT(setVisible(bool)));

QSize size(0, 0);
size = size.expandedTo(mainWidget->minimumSize());
resize(size);

setCentralWidget(mainWidget);
setWindowTitle("Testing middleWidget Expansion");

}

TestClass::~TestClass()
{

}


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

I even tried to add a public slot:


void TestClass::makeSmall(bool r)
{
middleWidget->setVisible(r);

QSize size(0, 0);
size = size.expandedTo(mainWidget->minimumSize());
resize(size);
}

and use

connect(moreButton, SIGNAL(toggled(bool)),
this, SLOT(makeSmall(bool))); insead but it doesn't work.

Thank you for your time.

wysota
12th December 2006, 07:28
The widget has to have the default size set to the size without the additional widgets. You should reimplement sizeHint() or do something simmilar.