Qt pass data between multiple forms

时间:2016-10-20 19:39:43

标签: qt

I am new to Qt and understands the concept of signal and slots. However I am not able to implement it.

My objective is:

Form1 has a button Config. So when I click Config it should open another form Form2( without closing Form1) and send a string strData to Form2.

In Form2 I set some value in the string strData. Then I click Ok button in Form2, Form2 should close and return back the string to Form1.

When the call returns back to Form1, it should continue from where it emitted the signal to invoke Form2.

Any help is highly appreciated.

1 个答案:

答案 0 :(得分:0)

你不能使用信号/插槽来做到这一点;发出信号,执行所有连接的插槽,然后代码从发出信号的地方继续,最终返回到事件循环。那时你的第二个表格实际显示出来并且用户可以对它做出响应,但到那时,你的代码已经远远超过了发出信号的位置。

我相信你正在寻找的是QDialog :: exec方法;用它代替信号。代码的基本模式是:

// This is the response to click on Config...
Form2Dialog form2;
form2.setSomeStringValue (some_value);

if (form2.exec() == QDialog::Accepted)
{
    QString some_new_value = form2.newValue();
}

Form2Dialog是QDialog的子类,您添加了自己的setSomeStringValue和newValue方法。 (你实际上这些名称取决于你。)

重要的是exec方法阻止并且在用户在对话框中选择“确定”或“取消”之前不会返回,或者使用标题栏中的“关闭”按钮将其关闭(如果有)。