Qsignalmapper This class is not a fresh concept, already exists in the QT2, and its function is consistent. However, due to the lack of propaganda (which is very rare in the example) it may not be a lot to know about this kind of person, so I hereby write about the function and usage of this class. simply understood, the Signalmapper class can be seen as a signal translator and forwarder, which can translate a signal without parameters into a signal with an int parameter, qstring parameter, qobject* parameter, or qwidget* parameter and forward it. So does anyone think that the scope of the class is applicable? Oh, is not a bit think of if I have a bunch of buttons, you can put the clicked event in a function to deal with, as long as the button to make a number or a button to a name on the line, so that you do not have to write a slot for each button, it is very convenient?
The following code implements this function:
/***mainwin.h***/
#ifndef Mainwin_h#defineMainwin_h#include<QWidget>classQsignalmapper;classMainwin: Publicqwidget{Q_object Public: Mainwin (Qwidget*parent =0); ~Mainwin ();PrivateSlots:voidDoclicked (ConstQString &btnname);Private: Qsignalmapper*Signalmapper;};#endif //Mainwin_h
/***mainwin.cpp***/
#include"Mainwin.h"#include<QSignalMapper>#include<QLayout>#include<QPushButton>#include<QMessageBox>Mainwin::mainwin (Qwidget*parent): Qwidget (parent) {QString ButtonText="Btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10";//10 x ButtonQstringlist texts = Buttontext.split (","); Signalmapper=NewQsignalmapper ( This); Qgridlayout*gridlayout =Newqgridlayout; for(inti =0; I < texts.size (); ++i) {Qpushbutton*button =NewQpushbutton (Texts[i]); Connect (button, SIGNAL (clicked ()), Signalmapper, SLOT (Map () ));//The original signal is passed to Signalmapper .Signalmapper->setmapping (button, texts[i]); //set the forwarding rules for the signalmapper, forward the signal to the qstring type of the parameter, and pass the contents of texts[i] as argument. Gridlayout->addwidget (button, I/3I3); The Connect (signalmapper, SIGNAL (mapped (ConstQString &)), This, SLOT (doclicked (ConstQString &));//connect the forwarded signal to the final slot functionsetlayout (gridLayout);}voidMainwin::d oclicked (Constqstring&btnname) {Qmessagebox::information ( This,"Clicked", btnname+"is clicked!");} Mainwin::~Mainwin () {}
/***main.cpp***/
#include <QApplication>"mainwin.h"int main (int Char *argv[]) { qapplication A (argc, argv); Mainwin W; W.show (); return a.exec ();}
From this example, the usage of qsignalmapper is very simple and easy to understand.
(1) First, the original signal is connected to the Signalmapper map slot function, so that Signalmapper can receive the original signal in the first time;
(2) Second call Setmapper method tells Signalmapper how to deal with the original signal. In this case, the original signal is converted into a signal with a qstring parameter.
(3) Finally receive the converted signal with the parameters, the converted signal is connected with the slot function, in the slot function to obtain the required data.
The function of the Qsignalmapper class is to create a mapping from the object that emits the original signal to the required data (the Setmapper function), and if your program happens to need such a function, then of course qsignalmapper is the best choice. In addition to the most common usage, let's think about what other occasions are appropriate to use this class.
In QT Examples There is an example of the Qsignalmapper class, in Examples/mainwindow/mdi/mainwindow.cpp. It uses the Qwidget pointer as an argument, and then the menu-selected signal is mapped to a pointer to a child window, which is eventually handled by Qmainwindow and is used to switch the subwindow. This usage is very some meaning, can say is the best use case of qsignalmapper, suggest everybody study. Here to intercept the most core of the code, there are comments should be good to understand, if you still have questions on the message:
//main class derived from QmainwindowMainwindow::mainwindow () {//...Windowmapper =NewQsignalmapper ( This); Connect (Windowmapper, SIGNAL (mapped (Qwidget*)), This, SLOT (Setactivesubwindow (Qwidget *))); //the forwarded signal is connected directly to the Setactivesubwindow slot of the Qmainwindow.//...}voidMainwindow::updatewindowmenu () {//... Qlist windows = mdiarea->subwindowlist ();Separatoract->setvisible (!windows.isempty ()); for(inti =0; I < windows.size (); ++i) {mdichild*child = Qobject_cast (windows.at (i)widget ()); QString text; if(I <9) {text= TR ("&%1%2"). Arg (i +1). Arg (child->userfriendlycurrentfile ()); } Else{text= TR ("%1%2"). Arg (i +1). Arg (child->userfriendlycurrentfile ()); } qaction*action = windowmenu->addaction (text); Action->setcheckable (true); Action->setchecked (Child = =activemdichild ()); Connect (action, SIGNAL (triggered ()), Windowmapper, SLOT (Map () ));//Monitor action's triggered signalWindowmapper->setmapping (Action, windows.at (i));//Create a mapping of the action pointer to qwidget*}//... }}<pre name="Code" class="CPP">voidMainwindow::setactivesubwindow (Qwidget *window)//Set the active child window{ if(!window)//If a widget is passed, set it as the active window return; UI->mdiarea->setactivesubwindow (Qobject_cast<qmdisubwindow *>(window));}
In addition, the signal mapper is also used in this example of C:\Qt\4.8.4\examples\tools\inputpanel.
Citation: http://blog.csdn.net/cuteqt/article/details/4306900
Transferred from: http://blog.csdn.net/zzwdkxx/article/details/28437441
Qsignalmapper-class method for processing multiple signals associated with the same slot (1)