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:
#include <QtGui/QApplication>
#include "testclass.h"
int main(int argc, char *argv[])
{
TestClass w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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>
{
Q_OBJECT
public:
TestClass
(QWidget *parent
= 0, Qt
::WFlags flags
= 0);
~TestClass();
private:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
testclass.cpp
#include "testclass.h"
TestClass
::TestClass(QWidget *parent, Qt
::WFlags flags
){
//topWidget
topWidget
= new QWidget(mainWidget
);
topLabel
= new QLabel(topWidget
);
topLabel->setText("This is the top widget");
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
);
middleLabel
= new QLabel(middleWidget
);
middleLabel->setText("This is the middle widget");
checkBox->setText("Extra Option");
middleLayout
->setSizeConstraint
(QLayout::SetFixedSize);
middleLayout->addWidget(middleLabel);
middleLayout->addWidget(checkBox);
//bottomWidget
bottomWidget
= new QWidget(mainWidget
);
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)));
size = size.expandedTo(mainWidget->minimumSize());
resize(size);
setCentralWidget(mainWidget);
setWindowTitle("Testing middleWidget Expansion");
}
TestClass::~TestClass()
{
}
#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()
{
}
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:
void TestClass::makeSmall(bool r)
{
middleWidget->setVisible(r);
size = size.expandedTo(mainWidget->minimumSize());
resize(size);
}
void TestClass::makeSmall(bool r)
{
middleWidget->setVisible(r);
QSize size(0, 0);
size = size.expandedTo(mainWidget->minimumSize());
resize(size);
}
To copy to clipboard, switch view to plain text mode
and use
connect(moreButton, SIGNAL(toggled(bool)),
this, SLOT(makeSmall(bool)));
connect(moreButton, SIGNAL(toggled(bool)),
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.
Bookmarks