如何从 QDialog 传递数据?

发布于 2024-09-16 10:11:41 字数 133 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-09-23 10:11:41

QDialog 有自己的消息循环,因为它会停止您的应用程序工作流程,所以我通常使用以下方案:

MyQDialog dialog(this);
dialog.setFoo("blah blah blah");
if(dialog.exec() == QDialog::Accepted){
  // You can access everything you need in dialog object
  QString bar = dialog.getFoo();
}

QDialog has its own message loop and since it stops your application workflow, I usually use the following scheme:

MyQDialog dialog(this);
dialog.setFoo("blah blah blah");
if(dialog.exec() == QDialog::Accepted){
  // You can access everything you need in dialog object
  QString bar = dialog.getFoo();
}
安静 2024-09-23 10:11:41

在一般情况下,如果数据相当简单,我喜欢创建一个或多个自定义信号并根据需要发出这些信号。简单或复杂的数据,我通常会提供数据的访问器。然后,在复杂的情况下,我会将一个槽连接到“已接受”信号,并在该槽中获取所需的信息。这样做的缺点是您通常需要依赖存储指向对话框的指针,或者使用 sender() hack 来确定哪个对象触发了该槽。

void Foo::showDialog()
{
    if ( !m_dlg )
    {
        m_dlg = new Dialog( this );
        connect( m_dlg, SIGNAL( accepted() ), SLOT( onDialogAccepted() ) );
    }
    m_dlg->Setup( m_bar, m_bat, m_baz );
    m_dlg->show();
}

void Foo::onDialogAccepted()
{
    m_bar = m_dlg->bar();
    m_bat = m_dlg->bat();
    m_baz = m_dlg->baz();
    // optionally destroy m_dlg here.
}

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 the sender() hack to figure out which object triggered the slot.

void Foo::showDialog()
{
    if ( !m_dlg )
    {
        m_dlg = new Dialog( this );
        connect( m_dlg, SIGNAL( accepted() ), SLOT( onDialogAccepted() ) );
    }
    m_dlg->Setup( m_bar, m_bat, m_baz );
    m_dlg->show();
}

void Foo::onDialogAccepted()
{
    m_bar = m_dlg->bar();
    m_bat = m_dlg->bat();
    m_baz = m_dlg->baz();
    // optionally destroy m_dlg here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文