Qt-interest Archive, March 2008
Re: Now I can ask my REAL question! -- resizing a window
Message 1 in thread
Hey Rich, I also had this problem.
I found that 'layout()->setSizeConstraint( QLayout::SetFixedSize );' worked great. It autosizes the main window for me.
By default, the size contraint is set to only GROW the window.
Tom Sirgedas
-----------------------
So, I now have a simple reproducer for a basic problem I have with Qt.
The below code displays a simple dialog that has a button on it that
shows another button if clicked. When the new button is clicked, it
hides that button.
I would like to tell the dialog to get smaller after hiding the
second button, exactly as if the user had dragged the dialog to make
it as small as the first button would let it. So I put a resize()
command in on_disappear_clicked(). Unfortunately, this has no
effect. The dialog size remains unchanged from when it had two
buttons. If I manually resize it with my mouse, then click "show"
again, it resizes to fit the second button, which is nice, but I want
it to shrink again when the second button disappears.
I want the dialog to remain resizable by the user, by the way.
Can anyone tell me why this doesn't work?
Thanks! This has been bugging me for weeks.
#include "QMainWindow"
#include "QPushButton"
#include "QHBoxLayout"
#include "QVBoxLayout"
class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent=NULL): QMainWindow(parent) {
setCentralWidget(new QWidget(this));
QVBoxLayout *layout = new QVBoxLayout;
changer = new QPushButton("show");
layout->addWidget(changer);
centralWidget()->setLayout(layout);
disappear = new QPushButton("disappear");
layout->addWidget(disappear);
disappear->hide();
connect(changer, SIGNAL(clicked()), this, SLOT(on_changer_clicked
()));
connect(disappear, SIGNAL(clicked()), this, SLOT
(on_disappear_clicked()));
return;
}
private slots:
void on_changer_clicked(){
printf("showing...\n");
disappear->show();
return;
}
void on_disappear_clicked() {
printf("hiding...\n");
disappear->hide();
resize(1,1);
}
public:
QPushButton *changer;
QPushButton *disappear;
};
#include "test.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow blah(NULL);
blah.show();
return app.exec();
}