Qt-Implementation of signals and slots with parameters (Entry level)

Source: Internet
Author: User

The first time I got started with QT, I had a headache because I only had the C language. The following example of QT signal and slot with parameters is only for beginners)

If you don't talk much about it, go directly to the Code:

/******** Mymainwindows. h **************/# ifndef mymainwindows_h _ # define mymainwindows_h _ # include <qwidget> # include <qpushbutton> # include <qlineedit> # include <qlabel> // declare a mymainwindows class, this class is used to implement form class mymainwindows: Public qwidget {q_object // q_object is a macro statement that must be included in the Custom signal and slot public: mymainwindows (); // class Constructor (used to implement the main part of the program) Public slots: // All slots must declare void slottest () in the public slots statement (); // This slot function is used to display and change when the button is clicked Change the text box content PRIVATE: qpushbutton * pb; // apply for a button qpushbutton qlineedit * ledit; // apply for a text box ledit qlabel * label; // apply for a text label Label signals: // All signals must be declared in the signals statement void sigtest (qstring text); // custom sigtest (qstring text) with parameters ). }; # Endif

/************* Mymainwindows. CPP ****************/# include "mymainwindows. H "mymainwindows: mymainwindows () {setgeometry (300,200,); Pb = new qpushbutton (" modify ", this); Pb-> setgeometry ); ledit = new qlineedit ("What's your name", this); ledit-> setgeometry (200,150,); label = new qlabel ("I Am a label ", this); label-> setgeometry (115,10, 100,20); Connect (Pb, signal (clicked (), this, slot (slottest ())); // connect the custom signal sigtest (qstring) to the predefined slot settext (qstring) connect (this, signal (sigtest (qstring), ledit, slot (settext (qstring);} // The Void mymainwindows: slottest () {label-> settext ("clicked "); // send custom signals. Note that the signal parameter types and quantities must be the same as those in the slot emit sigtest ("Hello, I am keyunchuan ");}

/************** Main. CPP ***************/# include <qapplication> // All QT applications must contain the qapplication header file # include "mymainwindows. H "int main (INT argc, char * argv []) {qapplication app (argc, argv); // declare a QT application like app mymainwindows W; // declare that we implement the mymainwindows form image W. w. show (); // display the W form return app.exe C ();}

Then attach the runtime:

Click Pb

Thank you for watching. If you have any improvement, please leave a message.

---------)

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.