Passing data between "Qt" forms (cross-control cross-Class), three cases and methods of processing

Source: Internet
Author: User
Tags emit

Environment: Qt5.5 MCVS2013
Ide:qtcreator
Sample code Download Address: http://download.csdn.net/detail/shihoongbo/9134859

Beginners who find a lot of Qt often get stuck on the question of how to pass data between forms, and the web is usually simply described as using a signal and slot (signal& slot) mechanism to pass
Although the transmission of signals and slots is true, it does not necessarily apply to all situations.
So, we summarize three kinds of cases and corresponding methods of passing data between forms:

Model Description:

Three forms are known, a is a parent control of B C, and B and C are sibling controls
Then the parameter passing is divided into three cases:
1.B passing parameters to a (c to a)
2.B passing parameters to C (c to B)
3.A passing parameters to B (A to C)

Scenario 1:
The "signals and slots" described on the web are passed by parameter, which is basically the case 1
Set a signal in class B and launch it, set the slot in a, and connect receives
B's Code

B.h
class B
{
...
Signals:
    void ToA ([paramlist]);
}

B.cpp
b::b ()
{
    emit ToA ([paramlist]);
}
     
     
      
      1
      
      2
      
      3
      
      4
      
      5
      
      6
      
      7
      
      8
      
      9
      
      ten
      
      13
     
     

A's code

A.h
class A
{
...
. Private:
   B *b;
Private slots:
   void Fromb ([paramlist]);
}

A.cpp
a::a ()
{
b=new A;
Connect (B,signal (ToA ([paramlist])), This,slot (Fromb ([paramlist]));
}
void A::fromb ([paramlist])
{
//get[paramlist]
}
     
     
      
      1
      
      2
      
      3
      
      4
      
      5
      
      6
      
      7
      
      8
      
      9-
      
      ten-
      
      20
     
     

Scenario 2:
About the transfer of data between two sub-forms under a form, the operation of sending signals in the corresponding subform, the operation of connecting signals and slots should be in main form A;
Suppose B sends data to C

A.h {... private:b *b;
C *c;
  }//a.cpp a::a () {b=new B;
  C=new C;

Connect (B,signal (ToC ([paramlist]), C,slot (Fromb ([paramlist])));}

B.h class B {... signals:void toc ([Paramlist]),}//b.cpp b::b () {Emit TOC ([paramlist]);}
     
     
      
      C.h class C {.... private slots:void Fromb ([paramlist]);}//c.cpp void C::fromb ([paramlist]) {//get[paramlist]}
      
      1 2 3 4 5 6 7
      
      8 9 10 11 12 13 14 15
      
      16 17 18 19 20 21 22
      
      23 24 25 26 27 28 29 30
      
      31 32 33 34 35 36 37 38
 39     
      40
     
      

Scenario 3:
About Case 3, from the parent form to the subform pass parameters, in the beginner Qt, tangled for a long time but can't find the method, in the subform, the parent form can not get a pointer, then connect when there is no way to specify the corresponding signal source. This problem, the Internet does not seem to find a corresponding solution.
Thought for a long time, it suddenly dawned, has been thinking of using QT "signal and groove" mechanism to carry out the data transmission between forms, but is limited to thinking. But forget the method of C + + itself, directly in the subform to leave a common member function, let the parent form to call, natural data transfer past, from the parent form to the subform (a->b/a->c) No need to use "signal and Groove" parameter transfer method
Suppose A->b sends data

B.h
class B
{
...
.. Public:
   void Froma ([paramlist]);
} 
B.cpp
void B::froma ([paramlist])
{
//get[paramlist]
}

//a.h
class A
{
.....
Private:
   B *b;
}
A.cpp
a:a ()
{
    b=new b;
    B->froma ([paramlist]);
}
     
     
      
      1
      
      2
      
      3
      
      4
      
      5
      
      6
      
      7
      
      8
      
      9
      
      Ten
      
      -
      
      15
      
      26 in all.
     
     

Summarize:
About the data transfer between QT forms is basically these three kinds of models
If it is across multiple forms (A->b->c->d ... ), then you need a layer of the sending signal receiver slot

When writing connect, it is also commonplace to note that the parameter list of the signal and slot needs to be matched (the Qt5 is that the parameters of the slot function must be less than the parameters of the signal function, and as to whether the QT4 has this characteristic, I do not specifically find the information)

A sample code download link at the beginning of the article that implements all the models of the case

Reprinted from: http://blog.csdn.net/shihoongbo/article/details/48681979

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.