[Qt-interest] Adding a QSpacerItem makes other widgets disapper...

Kustaa Nyholm Kustaa.Nyholm at planmeca.com
Tue May 25 14:51:11 CEST 2010


Hi,



I've got a piece of code as follows:

void MyView::buildUI() {
...

    while (!n2.isNull()) {
        QDomElement e = n2.toElement();
        if (!e.isNull()) {
            if ("pane" == e.tagName()) {
                QString className = e.attribute("class");
                QWidget* widget = 0;
                if ("calibrationPane" == className)
                    widget = new CalibrationPane(e);
                else if ("outputPane" == className)
                    widget = new OutputPane(e);
                else if ("inputPane" == className)
                    widget = new InputPane(e);

                if (widget)
                    layout()->addWidget(widget);
                else
                    qDebug() << "could not find def";
            }
        }
        n2 = n2.nextSibling();
    }

    //layout()->addItem(new QSpacerItem(20, 40, QSizePolicy::Expanding,
QSizePolicy::Expanding));



This is a piece of experimental code that builds up a user interface
by reading a def from an xml file. For each definition in the
xml file it instantiates one of three classes and adds them to the
the ui.

MyView is derived from QWidget, and so are CalibrationPane, OutputPane and
InputPane. Each of them also has a .ui form.

The above code works I get eg three panes nicely laid out vertically thanks
to 'new  QBoxLayout(QBoxLayout::TopToBottom, this)' in the MyView
constructor.

However, as soon as I uncomment the last line, the all the widgets that re
created int the while loop disappear. But I I add for example QPushButton's
before and/or after the 'addItem(new QSpacerItem' those are displayed
correctly.

So my thinking is that this is some sort of layout/hint/constraint issue but
I'm at lost on how to debug this.

I tried to display the widget as follows:

     QObject* t;
     QObjectList list = layout()->children();
     foreach (t , list) {
         QLayoutItem* item=reinterpret_cast<QLayoutItem*>(t);
         qDebug() << "***" << item->geometry();
     }

But the list seems to be empty, regardless if the widgets are
displayed correctly or not.

br Kusti






More information about the Qt-interest-old mailing list