简体   繁体   English

如何在Qt中的表单之间传输数据序列

[英]How to transfer data sequence between forms in Qt

Assume I have 2 forms in a Qt application. 假设我在Qt应用程序中有2种形式。 I need to transfer data between 2 forms many times so I used the loop to do this. 我需要在2种形式之间多次传输数据,因此我使用循环来完成此操作。 I just wanna get result from Form 2 before Form 1 is continue to loop. 我只是想在Form 1继续循环之前从Form 2获取结果。 This is my desired intention: 这是我想要的意图:

传输资料

When I used the loop normally, the loop began at i = 1 and sent data to Form 2. Before Form 2 returned result to Form 1, Form 1 was continue to send data with i = 2. 当我正常使用循环时,循环从i = 1开始并将数据发送到Form2。在Form 2将结果返回给Form 1之前,Form 1继续以i = 2发送数据。

Could you give me some solutions? 你能给我一些解决方案吗?

Thanks! 谢谢!

You can use signal slot concept to achieve this, 您可以使用信号插槽概念来实现这一点,

if form1 has object of form2 (Composition) 如果form1具有form2的对象(组成)

you can do 你可以做

 connect(this,SIGNAL(sendData(DataClass),ptr_form2,SLOT(receiveData(DataClass));
 connect(ptr_form2,SIGNAL(acknowledge()),this,SLOT(resendData()));

ptr_form2 is reference of form2 ptr_form2是form2的引用

DataClass is you data this could be anytype.replace it with your class object or any data type DataClass是您的数据,它可以是任何类型。用您的类对象或任何数据类型替换它

 //sendData is signal, in form1
 signals: void sendData(DataClass);

 //resendData is slot in form1
 private slots: void resendData()
 {
     emit sendData(objData); // objData is your data
 }

 //acknowledge is signal in form2
 signals: void acknowledge(); 

 //receiveData is public slot in form2
 public slots: void receiveData(DataClass data)
 {
     /* 
           Do you activity here
      */
     emit acknowledge(); // Emit Acknowledge
  }

if you have reference of both forms in any third form or class then you can do 如果您在任何第三种形式或类中都引用了这两种形式,则可以
let ptr_form1 is reference of form1 让ptr_form1是form1的引用
let ptr_form2 is reference of form2 让ptr_form2是form2的引用

 connect(ptr_form1 ,SIGNAL(sendData(DataClass),ptr_form2,SLOT(receiveData(DataClass));
 connect(ptr_form2,SIGNAL(acknowledge()),ptr_form1 ,SLOT(resendData()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM