如何在qt上访问父窗口小部件?
I have an inherited QTreeWidget (called PackList) class and its parent is a KXmlGuiWindow. How can I access to the parent's slots?
我有一个继承的QTreeWidget(称为PackList)类,其父级是KXmlGuiWindow。如何访问父母的插槽?
I've tried getParent()->mySlot() from the QTreeWidget class but I've got
我已经尝试过来自QTreeWidget类的getParent() - > mySlot()但我已经有了
error: no matching function for call to 'PackList::mySlot()'
Does anybody know the correct way? Thanks
有人知道正确的方法吗?谢谢
2 个解决方案
#1
12
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.
如果您知道父类,则必须将parentWidget()强制转换为该类,然后调用您的插槽。请记住,在这种情况下,它是否是一个插槽没有区别。你只是在调用一种方法。
((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.
最后,如果您不想强制转换它,可以使用QMetaObject :: invokeMethod来调用它。这可能是矫枉过正的。
更多相关文章
- 在Linux上确定陈旧的Samba挂载的最快方法
- 关于主机远程唤醒(WOL,Wake on Lan)的几种方法
- Linux下安装mysql 出现依赖包冲突解决方法
- linux线程函数中代替sleep的方法
- 提高Linux安全性--hosts.allow, hosts.deny 文件修改方法
- windows下virtualbox中Ubuntu与主机通过ssh建立连接的方法
- Linux产生随机数的几种常见方法
- 视频去广告及商用软件Vip功能破解方法
- Ubuntu 安装XAMPP集成环境软件包 与 运行WordPress 的简单方法