The Qsignalmapper of QT
- The Qsignalmapper of QT
- Briefly
- On the Code
- Related Knowledge points article
- End
Briefly
Qsignalmapper we can understand it as a transponder, what do we say? For example, the button clicks the response slot, binds to the Qsignalmapper, Qsignalmapper receives the button the click, and notifies the other control to do the processing. Some friends will ask, why so troublesome, need to turn a hand, can not remove the middle of the qsignalmapper, and directly call it. Of course, the answer is yes, why need to qsignalmapper this transponder, we first look at the effect, and then look at the code, I believe you will like Qsignalmapper this transponder.
On the Code
void Qsignalmapper_demo:: Initcontrol () {qvboxlayout*playout=New Qvboxlayout (this); QString Str="Button1 Button2 Button3 Button4 Button5"; Qstringlist strlist= str. Split (" "); Qsignalmapper*pmapper=New Qsignalmapper (this); int nrowcnt=0; foreach (QString itor, strlist) {Qpushbutton*pbtn=new Qpushbutton (this); Pbtn->setfixedheight (CON_HEGIHT); Pbtn->settext (itor); Connect (pbtn, SIGNAL (clicked ()), Pmapper, SLOT (map ())); Pmapper->setmapping (pbtn, Pbtn->text ()); PLayout->addwidget (pbtn, Nrowcnt++, 0);} Qlineedit *pedit = new QLineEdit (this ); Pedit->setfixedheight (CON_HEGIHT); Connect (pmapper, SIGNAL (mapped)), QString, SLOT (SetText (QString))); Playout->addwidget (PEdit, nrowcnt, 0); Playout->addstretch ();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
The vision of friends see my Code, code is not standard, say your code how to new, how not deltete, you do not have memory leak, here to tell everyone is not, oh, the relevant knowledge can see I wrote before the QT storage leakage management.
Back to this section, after reading the usage of qsignalmapper, we look back and see, do not qsignalmapper, and how to achieve the above effect, the edit box we can see, which button was pressed.
Remove Qsignalmappervoid Qsignalmapper_demo::initcontrol () {Qvboxlayout *playout =New Qvboxlayout (this); QStringstr ="Button1 Button2 Button3 Button4 Button5"; Qstringlist strlist =str.split (int nrowcnt = 0; foreach (QString itor, strlist) {Qpushbutton * PBTN = new Qpushbutton (this); Pbtn->setfixedheight (CON_ HEGIHT); Pbtn->settext (Itor); //here, the response is the same slot, when the button is clicked, we do not know which button was pressed. Connect (pbtn, SIGNAL (clicked ()), this, SLOT (onclicked () ())); //some friends said, can each button to bind a different groove to make a distinction ah, this can be done, but you think the use of qsignalmapper is more concise? Playout->addwidget (PBTN, nrowcnt++, 0);} Qlineedit *pedit = new qlineedit (this); pEdit-> Setfixedheight (CON_HEGIHT); Playout->addwidget (PEdit, nrowcnt, 0); Playout->addstretch ();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
After reading the code, I believe we all know the beauty of Qsignalmapper! Some friends ask again, when should I use Qsignalmapper? The above example is very simple, we need to deal with the same thing, and simplify a number of different slots but do the same thing, our qsignalmapper come in handy!
Related Knowledge points article
QT Storage leak management.
End
http://blog.csdn.net/ly305750665/article/details/53790209
Qt Qsignalmapper (can be understood as a transponder, multiple buttons are bound to an edit, and can be distinguished.) Each individual connection, instead of trouble)