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.
---------)