PDA

View Full Version : Hide contents of different layouts



zarkzervo
21st April 2009, 14:49
I have created several QGridLayout and put different labels and other widgets in these. As I choose an option in a QComboBox, I will hide all layouts but the one I have chosen, i.e. show different set of widgets for different options.

I have tried this:


test_layout = new QGridLayout();
test_layout->setColumnStretch(0,1);
test_labelOne = new QLabel("Angle");
test_labelOne->setParent(test_layout);
test_layout->addWidget(test_labelOne, 0, 1);


For then later traverse the test_layout->children() to set visibility on these children.

This gives me the compile-error:
ConfigureDlg.cpp:520: error: no matching function for call to âQLabel::setParent(QGridLayout*&)â
/usr/local/Trolltech/Qt-4.5.0-rc1/include/QtGui/qwidget.h:527: note: candidates are: void QWidget::setParent(QWidget*)
/usr/local/Trolltech/Qt-4.5.0-rc1/include/QtGui/qwidget.h:528: note: void QWidget::setParent(QWidget*, Qt::WindowFlags)

Any suggestions?

setParent(QObject*) is a function in QObject. and setParent(QWidget*) is a function in QWidget, but QWidget inherits QObject. Shouldn't it be available?

What I really try to accomplish is to have several set of layouts and hide and show these on demand to get a more clean code.

Lykurg
21st April 2009, 15:02
Hi,

couldn't you just delete row 4 with the set setParent? And how is test_layout defined? Maybe you have forgotten the "*":

QLabel::setParent(QGridLayout*&)
isn't right because it only will accept

QLabel::setParent(QGridLayout*)

Lykurg

zarkzervo
22nd April 2009, 09:52
Found a solution:


void ConfigureDlg::showChildren(const QGridLayout* layout, bool show)
{
QLayoutItem *item = 0;
QWidget *widget = 0;

for(int i = 0; i < layout->rowCount(); ++i)
{
for(int j = 0; j < layout->columnCount(); ++j)
{
item = layout->itemAtPosition(i,j);
widget=item?item->widget():0;
if(widget)
widget->setVisible(show);
}
}
}


item->widget() is a type safe casting function that returns 0 if the item is not a QWidget.

Each subset of tools are added to their own QGridLayout and all these layouts are added to a layout in my dialog widget with the same position.

Lykurg
22nd April 2009, 10:13
Wow, better you apply the layout to a QWidget/QFrame etc. and hide/show this.

zarkzervo
22nd April 2009, 14:59
Wow, better you apply the layout to a QWidget/QFrame etc. and hide/show this.

Ah. That was probably what I was looking for in the first place. But then I got another layer and some margins that need to be set, so I'll probably stick with my method now that I've already coded it.