Qt: sequentially emit multiple buttons and process their signals

Source: Internet
Author: User
Tags emit
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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.