The qsignalmapper class is not a new concept. It already exists as early as qt2, and its functions are consistent. However, due to insufficient publicity efforts (few involved in this example), we may not know much about this kind of people. Therefore, I would like to write an article to introduce such functions and usage.
The signalmapper class can be considered as a signal translation and repeater, it can translate a non-parametric signal into a signal with the int parameter, qstring parameter, qobject * parameter, or qwidget * parameter, and forward it. Do you think of the applicability of this class? Well, I thought about it. If I have a bunch of buttons, I can put the clicked event in a function for processing. I just need to encode a number for the button or name the button, in this way, it is not convenient to write a slot for each button?
The following code implements this function:
// Mainwin. h
Class mainwin: Public qwidget
{
Q_object
Public:
Mainwin (qwidget * parent = 0 );
Private slots:
Void doclicked (const qstring & btnname); // the slot for processing the final signal
PRIVATE: qsignalmapper * signalmapper;
};
// Mainwin. cpp
Mainwin: mainwin (qstringlist texts, qwidget * parent): qwidget (parent)
{
Qstring buttontext = "btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10"; // 10 buttons
Qstringlist texts = buttontext. Split (",");
Signalmapper = new qsignalmapper (this );
Qgridlayout * gridlayout = new qgridlayout;
For (INT I = 0; I <texts. Size (); ++ I)
{
Qpushbutton * button = new qpushbutton (texts [I]);
Connect (button, signal (clicked (), signalmapper, slot (MAP (); // transmit the original signal to signalmapper
Signalmapper-> setmapping (button, texts [I]);
// Set the forwarding rule of signalmapper, forward the signal with the qstring parameter, and pass the content of texts [I] as the real parameter.
Gridlayout-> addwidget (button, I/3, I % 3 );
}
Connect (signalmapper, signal (mapped (const qstring &), this, slot (doclicked (const qstring &); // connect the forwarded signal to the final slot function
Setlayout (gridlayout );
}
Void mainwin: doclicked (const qstring & btnname)
{
Qmessagebox: Information (this, "clicked", btnname + "is clicked! "); // Display the name of the BTN to be pressed.
}
From this example, the usage of qsignalmapper is very simple and easy to understand.
* First, connect the original signal without parameters to the map function of signalmapper, so that signalmapper can receive the original signal immediately;
* Call the setmapper method to tell signalmapper how to process the original signal. In this example, the original signal is converted into a signal with the qstring parameter.
* After receiving the converted parameter signal, connect the converted signal to the slot function to obtain the required data.
The core function of the qsignalmapper class is to build a seting (setmapper function) from the object that sends the original signal to the required data. If your program happens to need such a function, of course, qsignalmapper is the best choice. In addition to the most common usage above, let's start thinking about what other scenarios are suitable for this class?
The qsignalmapper class is used in only one example in examples of QT, in examples/mainwindow/MDI/mainwindow. cpp. It uses the qwidget pointer as a parameter, and then maps the signal selected from the menu to the pointer of the sub-window, which is finally processed by qmainwindow for subwindow switching. This usage is quite interesting. It can be said that it is the best use case for qsignalmapper. We suggest you learn it. Here we take the most core code, and you should understand it with comments. If there is still a problem, leave a message:
// The main class is derived from qmainwindow
Mainwindow: mainwindow ()
{
//...
Windowmapper = new qsignalmapper (this );
Connect (windowmapper, signal (mapped (qwidget *), this, slot (setactivesubwindow (qwidget *)));
// The forwarded signal is directly connected to the setactivesubwindow slot of qmainwindow.
//...
}
Void mainwindow: updatewindowmenu ()
{
//... Qlist windows = mdiarea-> subwindowlist ();
Separatoract-> setvisible (! Windows. isempty ());
For (INT I = 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 the triggered signal of Action windowmapper-> setmapping (action, windows. at (I); // establishes the action ing from the action pointer to qwidget *.
}
//...
}
The qsignalmapper class is not a new concept. It already exists as early as qt2, and its functions are consistent. However, due to insufficient publicity efforts (few involved in this example), we may not know much about this kind of people. Therefore, I would like to write an article to introduce such functions and usage.
The signalmapper class can be considered as a signal translation and repeater, it can translate a non-parametric signal into a signal with the int parameter, qstring parameter, qobject * parameter, or qwidget * parameter, and forward it. Do you think of the applicability of this class? Well, I thought about it. If I have a bunch of buttons, I can put the clicked event in a function for processing. I just need to encode a number for the button or name the button, in this way, it is not convenient to write a slot for each button?
The following code implements this function:
// Mainwin. h
Class mainwin: Public qwidget
{
Q_object
Public:
Mainwin (qwidget * parent = 0 );
Private slots:
Void doclicked (const qstring & btnname); // the slot for processing the final signal
PRIVATE: qsignalmapper * signalmapper;
};
// Mainwin. cpp
Mainwin: mainwin (qstringlist texts, qwidget * parent): qwidget (parent)
{
Qstring buttontext = "btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10"; // 10 buttons
Qstringlist texts = buttontext. Split (",");
Signalmapper = new qsignalmapper (this );
Qgridlayout * gridlayout = new qgridlayout;
For (INT I = 0; I <texts. Size (); ++ I)
{
Qpushbutton * button = new qpushbutton (texts [I]);
Connect (button, signal (clicked (), signalmapper, slot (MAP (); // transmit the original signal to signalmapper
Signalmapper-> setmapping (button, texts [I]);
// Set the forwarding rule of signalmapper, forward the signal with the qstring parameter, and pass the content of texts [I] as the real parameter.
Gridlayout-> addwidget (button, I/3, I % 3 );
}
Connect (signalmapper, signal (mapped (const qstring &), this, slot (doclicked (const qstring &); // connect the forwarded signal to the final slot function
Setlayout (gridlayout );
}
Void mainwin: doclicked (const qstring & btnname)
{
Qmessagebox: Information (this, "clicked", btnname + "is clicked! "); // Display the name of the BTN to be pressed.
}
From this example, the usage of qsignalmapper is very simple and easy to understand.
* First, connect the original signal without parameters to the map function of signalmapper, so that signalmapper can receive the original signal immediately;
* Call the setmapper method to tell signalmapper how to process the original signal. In this example, the original signal is converted into a signal with the qstring parameter.
* After receiving the converted parameter signal, connect the converted signal to the slot function to obtain the required data.
The core function of the qsignalmapper class is to build a seting (setmapper function) from the object that sends the original signal to the required data. If your program happens to need such a function, of course, qsignalmapper is the best choice. In addition to the most common usage above, let's start thinking about what other scenarios are suitable for this class?
The qsignalmapper class is used in only one example in examples of QT, in examples/mainwindow/MDI/mainwindow. cpp. It uses the qwidget pointer as a parameter, and then maps the signal selected from the menu to the pointer of the sub-window, which is finally processed by qmainwindow for subwindow switching. This usage is quite interesting. It can be said that it is the best use case for qsignalmapper. We suggest you learn it. Here we take the most core code, and you should understand it with comments. If there is still a problem, leave a message:
// The main class is derived from qmainwindow
Mainwindow: mainwindow ()
{
//...
Windowmapper = new qsignalmapper (this );
Connect (windowmapper, signal (mapped (qwidget *), this, slot (setactivesubwindow (qwidget *)));
// The forwarded signal is directly connected to the setactivesubwindow slot of qmainwindow.
//...
}
Void mainwindow: updatewindowmenu ()
{
//... Qlist windows = mdiarea-> subwindowlist ();
Separatoract-> setvisible (! Windows. isempty ());
For (INT I = 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 the triggered signal of Action windowmapper-> setmapping (action, windows. at (I); // establishes the action ing from the action pointer to qwidget *.
}
//...
}