PDA

View Full Version : A bug of QLayout



cocalele
24th April 2006, 07:21
Hi,
I just find a assert failure when I use QLayout. I want know is this a bug of QLayout.
The QLayout has a constructor like this, which need a parent of QWdiget


QLayout::QLayoutQWidget *parent)
: QObject(*new QLayoutPrivate, parent)
{
if (!parent)
return;
parent->setLayout(this);
}


But in the code of QLayout::parentWidget() (line 6), it trys to cast the parent to QLayout


QWidget *QLayout::parentWidget() const
{
Q_D(const QLayout);
if (!d->topLevel) {
if (parent()) {
QLayout *parentLayout = ::qobject_cast<QLayout*>(parent());
Q_ASSERT(parentLayout);
return parentLayout->parentWidget();
} else {
return 0;
}
} else {
Q_ASSERT(parent() && parent()->isWidgetType());
return static_cast<QWidget *>(parent());
}
}


certainly, cast from QWidget* to QLayout* will failled, and the successed assertion will failed. Is this a bug or anything I don't know?
Thanks for your help!

dimitri
24th April 2006, 07:51
Could you provide a minimal example that reproduces the problem? I don't think there's a bug (the parent constructor argument and parent() are different) but it's hard to tell without some code to reproduce the failed assertion.

cocalele
25th April 2006, 02:54
here's some code reproduce the error


HelloQt::HelloQt(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
QHBoxLayout *topMostLayout = new QHBoxLayout(this);

//QVBoxLayout *subLayout = new QVBoxLayout(this); //addLayout will fail
QVBoxLayout *subLayout = new QVBoxLayout(); //It's OK

topMostLayout->addLayout(subLayout);

}

dimitri
25th April 2006, 21:10
I mean a minimal complete program that I could build to reproduce what you're seeing.

jpn
25th April 2006, 21:25
QHBoxLayout *topMostLayout = new QHBoxLayout(this);
QVBoxLayout *subLayout = new QVBoxLayout(this); //addLayout will fail


This is non-sense. Passing a parent widget to layout constructor is same than setting the layout as widget's layout. So what the code above does is it tries to set 2 different layouts one after another to a same widget.


Attempting to add QLayout "" to QDialog "", which already has a layout.

Anyhow, all it does for me is showing the warning. No assertion fails for me with Qt 4.1.2..
Which version are you using?

Edit: Check this task tracker entry (http://www.trolltech.com/developer/tasktracker.html?method=entry&id=91614).

cocalele
26th April 2006, 02:18
I mean a minimal complete program that I could build to reproduce what you're seeing.

Thanks for your help! I have attached my whole test code in HelloQt.zip.

The assert fail is not in the constructor of subLayout, but in the code add sublayout to the topmost layout.

I use Qt4.1.0

jpn
26th April 2006, 06:10
Did you check the task tracker entry link I provided? This has already been fixed in Qt 4.1.1.
Changes 4.1.1 (http://www.trolltech.com/developer/changes/changes-4.1.1.html): "QLayout: Warn instead of crash when adding two layouts to a widget."

So consider updating to the newest Qt.