动态添加叠加微件

Dynamically add overlay widgets

本文关键字:叠加 添加 动态      更新时间:2023-10-16

是否可以将动态的新子小部件添加到父小部件?

我有以下代码:

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

矢量m_otherWidgets包含子小部件的列表。问题是它只显示这个子小部件,这些小部件是在构造函数时创建的。

没有更多信息我只能猜测,但你可能忘了打电话给show()/setVisible(true).显示父项后添加的小组件并不总是显示。