How to access to parent widget on qt?

后端 未结 2 1544
青春惊慌失措
青春惊慌失措 2021-01-12 15:23

I have an inherited QTreeWidget (called PackList) class and its parent is a KXmlGuiWindow. How can I access to the parent\'s slots?

I\'ve tried getParent()->mySlot

2条回答
  •  别那么骄傲
    2021-01-12 15:50

    If you know the parent's class, you will have to cast parentWidget() to that class and then call your slot. Keep in mind whether or not it's a slot makes no difference in this case. You are just calling a method.

    ((KXmlGuiWindow*)parentWidget())->mySlot();
    

    You can make the call without casting by wiring up your signal to the slot.

    connect( this, SIGNAL(mySignal()), parentWidget(), SLOT(mySlot()) );
    

    Lastly, you can use QMetaObject::invokeMethod to call it if you don't want to cast it. That's probably overkill.

提交回复
热议问题