Qt 3 - adding a widget to an already existing widget and its layout

1k views Asked by At

I have a QDialog created in designer with a QFrame testFrame which I've added a QHBoxLayout to, and at some point after the whole thing has been created and shown I am trying to programatically create a new widget with the QFrame as a parent. Whatever I try the new widget won't display:

End of init() method:

QBoxLayout *testFrameLayout = new QHBoxLayout(this->testFrame); //QT3 docs use QBoxLayout pointer when creating QHBoxLayout

During an event (e.g. a separate button has been pressed)

testButton = new QPushButton(this->testFrame);
testButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, false);
testButton->setMaximumWidth(50);
testButton->setMaximumHeight(50);
testButton->setMinimumWidth(50);
testButton->setMinimumHeight(50);

testFrameLayout->addWidget(testButton);

I get this behaviour with different widgets, container classes (e.g. QGrid where I don't have to manage any layout stuff), calling update()/repaint() (on testFrame, testButton, the dialog itself) e.t.c, whatever I try it works when done in init() but not if done after the dialog has been displayed. I must be missing something, so any help would be appreciated!

Note: I am working on a very large project that uses Qt 3, so solutions must be compatible with this version.

2

There are 2 answers

0
Rajveer On BEST ANSWER

Calling

testButton->show();

at the end solved this problem. Since using adjustSize() on the frame without show() on the actual button made the frame disappear, I think that maybe widgets are hidden until explicitly shown when added in like this.

1
koan On

Here is some working code from a dialog in my Qt3 application:

QVBoxLayout *vb = new QVBoxLayout(this, 5, 5);
// ...
QHBoxLayout *askline = new QHBoxLayout(vb, 10);
QPushButton *ok      = new QPushButton(tr("OK"), this);
QPushButton *cancel  = new QPushButton(tr("Cancel"), this);
askline->addWidget(ok);
askline->addWidget(cancel);
adjustSize();
// ...

I think your problem is that the layout hasn't allocated any space for your QPushButton and the adjustSize() is needed.