PDA

View Full Version : Widgets taking too much space hidden on a Layout



mrlinx
1st September 2011, 06:20
So, I have a QDockWidget with 10 widgets inside... but using code, the first thing I do when I start my app is to hide all but one. Still, can't figure out a way to make my dock small. It always takes at minimum the size of all widgets being shown (although they are hidden).

My question(s) therefore is:
- Is remove/add better than hide/show for layout size managing?
- Does the size calculated automatically by the QDesigner (visible inside the .ui file) changes anything about layout size?

wysota
1st September 2011, 08:36
Which layout are you using? When are you hiding the widgets?

mrlinx
1st September 2011, 16:20
Thanks for the interest.

The dock panel has a vertical layout holding 10 widgets. All this widgets are basically horizontal layouts with buttons.
I'm hiding the inside widgets on the window constructor (widget1->hide()).

The idea is that the panel has some buttons (one widget) showing for each state of my application, but the dock should always have the height of a line of buttons, never the height of 10 lines, like is currently taking.

yeye_olive
1st September 2011, 16:33
I do not have the explanation for the behaviour you are experiencing, but I have a suggestion. Apparently exactly one widget among the 10 must be visible at any given time. Instead of showing and hiding the widgets manually, I suggest you put them all in a QStackedLayout.

mrlinx
1st September 2011, 16:48
Well, I simplified the problem by not saying that there's a state where I'm actually showing 2 at a time. That shouldn't make a difference on the size because when the layout is first created, all widgets are hidden but one, and the height is still big... this should make the qstackedlayout a non-option :/

ChrisW67
2nd September 2011, 00:19
Use QLayout::setSizeConstraint() to force the layout size to follow the size hint of visible rows of buttons.


#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"

Try it with and without line 11. It does constrain both directions though.