简体   繁体   中英

Dynamically add overlay widgets

Is it possible to add dynamically new child widget to the parent?

I have the following code:

MyWidget : public QWidget
{
    MyWidget() : Qwidget() 
    {
        m_otherWidgets.push_back( new OtherWidget(this) ); // this will be painted
    }

    void addNew()
    {
        m_otherWidgets.push_back( new OtherWidget(this) ); // this will not be painted
    }

    std::vector<OtherWidget*> m_otherWidgets;
}

MyWidget bar(); // 1 other widget painted
bar.addNew(); // still only 1 other widget painted

Vector m_otherWidgets contains a list of child widgets. The problem is that it display only this child widgets, that were created during constructor time.

Without more information I can only guess, but you probably forgot to call show() / setVisible(true) . Widgets added after the parent is shown are not always displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM