[Qt-interest] Add custom frame to existing layout at runtime

Andras Toth parrotfortytwo at gmail.com
Sat Dec 5 18:13:34 CET 2009


Hello,

There is a frame created with the designer built into Qt Creator,
containing some widgets. I would like to create more instances of this
frame during run-time, and add them to an existing Vertical Layout,
which was statically added to another frame with the designer.
The problem is, that when the code for adding the frame executes, the
new instance of the custom form does not show up. I know something is
happening though, because a QPushButton already contained in the
layout, shift upwards.
I have tried adding also a normal QPushButton in the same way, and it
_does_ appear. I'm sure I am doing some trivial mistake, but I've been
struggling and googling after it, and have found nothing useful.

The code:

Adding the new custom frame dynamically:

void SomeWindow::on_PushButton1_clicked()
{
   CustomFrame * new_frame = new CustomFrame;
    m_ui->myLayout->addWidget(new_frame);             // does not appear
   new_frame->show();

    QPushButton * pb = new QPushButton("hello");
    m_ui->myLayout->addWidget(pb);                        // this does appear
}


Header of the custom frame created with Qt Creator:

class CustomFrame : public QFrame {
    Q_OBJECT
public:
    CustomFrame(QWidget *parent = 0);
    ~CustomFrame();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::CustomFrame *m_ui;
};


Thanks for any help,

Andras Toth



More information about the Qt-interest-old mailing list