简体   繁体   English

在运行时添加自定义QWidget

[英]Adding custom QWidget during runtime

I'm trying to implement a custom widget hierarchy: QMainWindow -> QFrame -> MyWidget -> QFrame -> MySubWidget 我想实现一个自定义窗口小部件层次: QMainWindow - > QFrame - > MyWidget - > QFrame - > MySubWidget

Here is how MyWidget class looks like: 这是MyWidget类的样子:

    class MyWidget : public QWidget {
        Q_OBJECT
    public:
        MyWidget(QWidget *parent = 0, ...);
        ...
    public slots:
        void SlotFunction(int i);
        ...
    private:
        MySubWidget *sub_w;
        QFrame *sub_frame;
        ...
    }

If I try to create an MySubWidget during MyWidget constructor, then all MySubWidget elements are shown as intended: 如果尝试在MyWidget构造函数中创建MySubWidget,则所有MySubWidget元素均按预期显示:

    MyWidget::MyWidget (...) : QWidget(parent) {
        ...
        sub_frame = new QFrame(this);
        ...
        sub_w = new MySubWidget(sub_frame); // commented out on a runtime test
    }

But if I try to add subwidget during runtime, sub_frame remains blank. 但是,如果我尝试在运行时添加subwidget,sub_frame仍为空白。 Ie signal reaction: 即信号反应:

    void MyWidget::SlotFunction(int i) {
        sub_w = new MySubWidget(sub_frame); // update, repaint, show and hide methods aren't helphul
    }

I know this is an old question, but I was having a very similar issue and it turned out to be a lack of call to the QWidget::show(). 我知道这是一个老问题,但是我遇到了一个非常类似的问题,结果是缺少对QWidget :: show()的调用。 Perhaps that was your problem as well? 也许那也是您的问题?

My question here: Dynamically add instance inherited from QWidget 我的问题在这里: 动态添加从QWidget继承的实例

Cheers. 干杯。

Are you reaching your function? 您正在履行职责吗?

At the top of your function before making a new instance of MySubWidget put: 在创建MySubWidgetnew实例之前,将函数放在顶部:

qDebug() << Q_FUNC_INFO;

Is the slot connected properly? 插槽是否正确连接?

Qt will let you know if it is unable to connect a slot using a runtime warning. Qt将使用运行时警告通知您是否无法连接插槽。 Look at the debug output that shows up in Qt Creator and it may mention a reason why the slot was never reached. 查看Qt Creator中显示的调试输出,它可能会提到从未到达插槽的原因。

Is subframe visible? subframe可见吗?

If the parent of your object isn't visible, then showing or hiding the child object will only affect it when the parent is shown. 如果对象的父对象不可见,则仅当显示父对象时,显示或隐藏子对象才会对其产生影响。

Hope that helps. 希望能有所帮助。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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