It is a good technique to arrange multiple buttons like QQ's friend list. qtoolbox can be used to simply implement this requirement, the qsignalmapper class using QT can easily process a large number of signals sent by different controls. The following is a small example based on these two technologies.
Ideas:
1: create multiple buttons (or other controls) and put them in a group.
2: connect the clicked signal of the button to signalmapper and set their "signal conversion rules.
3: connect the mapped signal of signalmapper to the clickedbutton ).
Program:
Source code:
# Include <qtgui> # include <qtcore> const int number = 100; Class buttonsbox: Public qtoolbox {q_objectprivate: qpushbutton * button [number]; qsignalmapper * signalmapper; public: buttonsbox (qwidget * parent = 0); Public slots: void clickedbutton (const qstring & text) ;}; buttonsbox: buttonsbox (qwidget * parent/* = 0 */): qtoolbox (parent) {signalmapper = new qsignalmapper (this); qgroupbox * groupbox = new qgroupbox; q Vboxlayout * sublayout = new qvboxlayout (groupbox); // connect (signalmapper, signal (mapped (const qstring &), this, slot (clickedbutton (const qstring &); For (INT I = 0; I <number; I ++) {qstring text = qstring: Number (I ); button [I] = new qpushbutton (this); button [I]-> settext (text); sublayout-> addwidget (Button [I]); // original signal transmitted to signalmapperconnect (Button [I], signal (clicked (), signalmapper, slot (MAP ()));// Set the forwarding rule of signalmapper, forward it as a signal of qstring type, and pass the text content as a parameter. Signalmapper-> setmapping (Button [I], text);} additem (groupbox, "All buttons");} void buttonsbox: clickedbutton (const qstring & text) {qmessagebox :: information (this, "OK", text) ;}# include "Main. MOC "int main (INT argc, char ** argv) {qapplication app (argc, argv); buttonsbox * toolbox = new buttonsbox; toolbox-> show (); return app.exe C ();}