Use QLayout::setSizeConstraint() to force the layout size to follow the size hint of visible rows of buttons.
#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
setLayout(vlayout);
vlayout
->setSizeConstraint
(QLayout::SetFixedSize);
for (int r = 0; r < 3; ++r) {
// Construct a new row
row->setLayout(hlayout);
for (int c = 0; c < 5; ++c) {
hlayout->addWidget(p);
}
vlayout->addWidget(row);
rows << row;
}
// Hide all but first row
for (int r = 1; r < rows.count(); ++r)
rows.at(r)->hide();
// Trigger some changes in visibility
QTimer::singleShot(3000,
this,
SLOT(showSecond
()));
QTimer::singleShot(8000,
this,
SLOT(hideSecond
()));
}
public slots:
void showSecond() { rows.at(1)->show(); }
void hideSecond() {
rows.at(0)->hide();
rows.at(1)->hide();
rows.at(2)->show();
}
private:
QWidgetList rows;
};
int main(int argc, char *argv[])
{
Widget w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class Widget: public QWidget {
Q_OBJECT
public:
Widget(QWidget *p = 0): QWidget(p) {
QVBoxLayout *vlayout = new QVBoxLayout;
setLayout(vlayout);
vlayout->setSizeConstraint(QLayout::SetFixedSize);
for (int r = 0; r < 3; ++r) {
// Construct a new row
QWidget *row = new QWidget(this);
QHBoxLayout *hlayout = new QHBoxLayout;
row->setLayout(hlayout);
for (int c = 0; c < 5; ++c) {
QPushButton *p = new QPushButton(QString("R%1C%2").arg(r).arg(c), row);
hlayout->addWidget(p);
}
vlayout->addWidget(row);
rows << row;
}
// Hide all but first row
for (int r = 1; r < rows.count(); ++r)
rows.at(r)->hide();
// Trigger some changes in visibility
QTimer::singleShot(3000, this, SLOT(showSecond()));
QTimer::singleShot(8000, this, SLOT(hideSecond()));
}
public slots:
void showSecond() { rows.at(1)->show(); }
void hideSecond() {
rows.at(0)->hide();
rows.at(1)->hide();
rows.at(2)->show();
}
private:
QWidgetList rows;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
#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.
Bookmarks