How to access to parent widget on qt?

半世苍凉 提交于 2019-12-01 04:40:22

问题


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() from the QTreeWidget class but I've got

error: no matching function for call to 'PackList::mySlot()'

Does anybody know the correct way? Thanks


回答1:


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.




回答2:


I'm not sure I fully understand your question.

However, you can access the parent widget of a widget with parentWidget().

Then, you should be able to call any public slot :

parentWidget()->a_slot();


来源:https://stackoverflow.com/questions/352758/how-to-access-to-parent-widget-on-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!