QT When a lot of buttons defined, and their signals are the same (such as the click Signal), there is no need for each of them to set the signal and slot links, qsignalmapper to solve this problem.
today to complete this similar function, do not know how to do, asked the next degree Niang, the results have been achieved, the effect is simply too perfect, if not to do so, I really do not know how to do.
in the completion of the process, there has been a problem, see a half-day also did not solve, I was confused, and the online example is exactly the same, but can not be implemented, finally saw the next function prototype, the original and I want to ship parameters do not match, I need is an int parameter, And to pass the time is qstring type, really depressed Ah!
The main steps are as follows:
1) Define a Qsignalmapper object
Qsignalmapper *signalmapper = new Qsignalmapper (this);
2) Create a link with no parameters
Connect (tool_button,signal (clicked ()), Signalmapper,slot (Map ()));
3) to convert
Signalmapper->setmapping (Tool_button,nindex);
Note: Be careful when converting, convert according to your needs, setmapping () functionparameter is not arbitrary, the second parameter is the parameter you want to convert, I need int type, in the loopNindex is plastic, or in the back of the signal and Groove link will be very bitter force!
4) Finally we forward the signal to the final processing function slot_turnpage (int) (signal slot)
Connect (signalmapper,signal (mapped (int)), This,slot (slot_turnpage (int)));
5) perform the corresponding operation in the slot function. such as
void toolbar::slot_turnpage (int current_page)
{
Switch (current_page) {
Case 0:
Qdebug () << "1th button is pressed";
Break
Case 1:
Qdebug () << "2nd button is pressed";
Break
Default
Break
}
}
Qsignalmapper It can translate a non-parametric signal into a signal with an int parameter, qstring parameter, qobject* parameter, or qwidget* parameter, and in a specific project we can do the corresponding conversion as needed.
Use of the Qsignalmapper class