如何从 QDialog 传递数据?
在 Qt 中,当您需要传递比布尔值或整数返回码更复杂的内容时,将数据从 QDialog 子类传递到启动对话框的组件的最优雅的方法是什么?
我正在考虑从 accept()
插槽发出自定义信号,但是还有其他东西吗?
In Qt, what is the most elegant way to pass data from a QDialog subclass to the component that started the dialog in the cases where you need to pass down something more complex than a boolean or an integer return code?
I'm thinking emit a custom signal from the accept()
slot but is there something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QDialog 有自己的消息循环,因为它会停止您的应用程序工作流程,所以我通常使用以下方案:
QDialog has its own message loop and since it stops your application workflow, I usually use the following scheme:
在一般情况下,如果数据相当简单,我喜欢创建一个或多个自定义信号并根据需要发出这些信号。简单或复杂的数据,我通常会提供数据的访问器。然后,在复杂的情况下,我会将一个槽连接到“已接受”信号,并在该槽中获取所需的信息。这样做的缺点是您通常需要依赖存储指向对话框的指针,或者使用
sender()
hack 来确定哪个对象触发了该槽。In the general case, if the data is fairly simple I like to create one or more custom signals and emit those as necessary. Simple or complex data, I will generally provide accessors for the data. In the complex case, then, I would connect a slot to the
accepted
signal, and get the desired information in that slot. The drawback to this is that you generally need to rely on storing a pointer to the dialog, or using thesender()
hack to figure out which object triggered the slot.