Use qsignalmapper to classify and process a large number of signals in an orderly manner (including the source code)

Source: Internet
Author: User

 

All posts on this site are reserved by this site and the original author. This article can only be reproduced if the copyright information, original article links, and original article authors are retained. Do not delete or modify the original article content for reprinting, it is not intended for commercial purposes. Thank you for your cooperation. Original article: Uses qsignalmapper to classify and process a large number of signals (including source code) in an orderly manner)

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. hclass mainwin: Public qwidget {q_objectpublic: mainwin (qwidget * parent = 0); Private slots: void doclicked (const qstring & btnname); // the slot for processing the final signal is private: qsignalmapper * signalmapper;}; // mainwin. signature: 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-> setmapping (button, texts [I]); // set forwarding rules for signalmapper, forward as a signal of the qstring type and pass the content of texts [I] as a 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 derives mainwindow: mainwindow () from qmainwindow (){//... 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); // create a action ing from the action pointer to qwidget //...}

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.