Qt4 custom slot and Signal
The code shown in this article is successfully debugged in Windows XP + mingw3.4.2 + eclipse3.4 + cdt5.0
The QT System provides a comprehensive Signal Processing Mechanism for us, and its internal signal and slot are already very comprehensive. Therefore, in most cases, we do not need to design signals and slots by ourselves, however, sometimes we may need to design some unique signals and slots to meet the needs of the program. Qt's core mechanism signal and slot are independent of C ++. Generally, C ++ is designed to handle buttons such as Button clicking. Callback functions are mostly used, which is complicated in callback function design, although the execution efficiency is higher than that of QT signal and slot, its conversion is prone to errors. Moreover, in daily application design, the difference in efficiency is not proportional. Therefore, QT uses its own dedicated design for efficient programming. This article also advocates the use of signals and slots.
How can we design your own signals and slots? After reading this section, you can easily meet your needs.
Before creating signals and slots, you must first create three files: class declaration file (yourclass. h), Class implementation file (youclass. CPP), the main program file (main. CPP ).
Let's take a look at the example below. Suppose we need a program to process the content of the text box. The main interface of the program has two buttons. When we click the button [modify, the text box is changed to "Jingjing ".
1. Custom slot:
The button click signal clilcked () already exists through inheritance, so we do not need to define this signal for the time being. To change the text box content, we need to define a slot to respond to the click signal.
Sample Code (yourclass. h ):
Copy code
# Ifndef yourclass_h _
# Define yourclass_h _
# Include <qwidget> // files to be included in the QT Window application
# Include <qpushbutton> // You must include
# Include <qlineedit> // You must include
Class mymainwindows: Public qwidget // declare a mymainwindows class, which is used to implement the form
{
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 be declared in the public slots statement.
Void slottest (); // This slot function is used to display the content of the text box when the button is clicked.
PRIVATE:
Qpushbutton * pb; // apply for a button qpushbutton
Qlineedit * ledit; // apply for a text box ledit
};
# Endif/* yourclass_h _*/
In the preceding sample code, we declare an application class file in a window. Its function is to declare all the element information required by the program. In this class, we declare our slot slottest () function to respond to the signal clicked by the button. As follows:
Copy code
Public slots: // All slots must be declared in the public slots statement.
Void slottest (); // This slot function is used to display the content of the text box when the button is clicked.
As noted in the Code, there are two important points in the Custom slot and signal:
First: All slot functions must be included in the public slots statement. Its declaration is similar to the Public Function Method pulic of the class. If you have multiple slot functions, you only need to list them one by one.
Second, the class file should contain the q_object macro statement at the beginning. Remember that it does not require punctuation, so do not add the [;] symbol. All QT User-Defined slots and signals must exist.
The following is the second file: yourclass. cpp. This file is used in the Implementation part of the element stated in yourclass. h.
Sample Code (class implementation file yourclass. cpp)
Copy code
# Include "yourclass. H" // include yourclass. H class declarative File
Mymainwindows: mymainwindows () // implement the constructor
{
Setgeometry (300,200, 300); // draw the main form. The size is 200 x.
PB = new qpushbutton ("modify", this); // implement the Pb button, which is designed as modify
Pb-> setgeometry (10, 10,); // draw the location (10, 10) and size () of the Pb button)
Ledit = new qlineedit ("What's your name", this); // implement the ledit file box with the content of what's your name
Ledit-> setgeometry (10, 30, 200,150); // draw the location (10, 30) and size (200,150) of the ledit file box)
Connect (Pb, signal (clicked (), this, slot (slottest (); // click the clicked () signal and custom slot slottest () connected to the Pb button ()
}
Void mymainwindows: slottest () // Implementation of the custom slot function, reset the content of the text box
{
Ledit-> settext ("Jingjing"); // "Jingjing" indicates the content after the modify button is clicked.
}
In yourclass. cpp, we implement all the functions of the program, and an entry is required for program execution. Its body is mian. cpp.
Copy code
# Include <qapplication> // All QT applications must contain the qapplication header file.
# Include "yourclass. H" // contains the class definition file yourclass. h
Int main (INT argc, char * argv [])
{
Qapplication app (argc, argv); // declare a QT Application
Mymainwindows W; // declare that we implement the mymainwindows form image W.
W. Show (); // display the W form
Return app.exe C ();
}
After the program is compiled, run the command. When you click the modify button, the text content "What's your name" is changed to Jingjing.
2. Custom Signal
In the above example, we learned how to implement our own slot functions. Next we will introduce how to implement our own signals. The declaration and implementation of QT signals are easier than those of QT functions. It is equivalent to a series of function prototypes. No specific implementation is required. In the preceding section, we click the clicked () signal to connect to the slot function slottest () and modify the content of the text box. In this example, we will add a text label in the form, and then test whether the label content has changed in the slot function slottest (). If the label content has changed, the signal sigtest () is triggered (). Connect the signal to the slot function slottest2 () and change the text box content to "sigsig ".
The following is the implementation file:
Open the yourclass. h file in the previous section and add a slot function slottest2 () and a signal sigtest () to the code (). The red code is added as follows:
Copy code
# Ifndef yourclass_h _
# Define yourclass_h _
# Include <qwidget> // files to be included in the QT Window application
# Include <qpushbutton> // You must include
# Include <qlineedit> // You must include
# Include <qlabel> // You must include
Class mymainwindows: Public qwidget // declare a mymainwindows class, which is used to implement the form
{
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 be declared in the public slots statement.
Void slottest (); // This slot function is used to display the content of the text box when the button is clicked.
Void slottest2 (); // added a slot function to change the content of the text box to sigsig.
PRIVATE:
Qpushbutton * pb; // apply for a button qpushbutton
Qlineedit * ledit; // apply for a text box ledit
Qlabel * label; // apply for a text label
Signals: // All signals must be stated in the signals statement.
Void sigtest () // Add new signal sigtest ().
};
# Endif/* yourclass_h _*/
In this example, we add the custom signal sigtest (). In QT, the signal statement is added to the class. Then define your own signal function after the statement. Interestingly, the definition of signals does not need to be implemented, just like a series of function prototypes. You only need to apply. In this example, we applied for a signal sigtest () without any parameters (). In the following section, we will use this signal to change the content of the text box.
To draw text labels on the form, you must add the <qlabel> header file. Next, we add a slot slottest2 () function and a signal sigtest () function in the code, and connect them through the connect () function.
The Code is as follows (yourclass. cpp ):
Copy code
# Include "yourclass. H"
Mymainwindows: mymainwindows ()
{
Setgeometry (300,200 );
PB = new qpushbutton ("modify", this );
Pb-> setgeometry (10, 10 );
Ledit = new qlineedit ("What's your name", this );
Ledit-> setgeometry (200,150 );
Label = new qlabel ("My meme", this); // implement a text label and set its content to my Meme
Label-> setgeometry (115,10, 100,20 );
Connect (Pb, signal (clicked (), this, slot (slottest ()));
Connect (this, signal (sigtest (), this, slot (slottest2 (); // connects the custom signal sigtest () and slottest2 () slot Functions
}
Void mymainwindows: slottest () // signal 1
{
Ledit-> settext ("Jingjing ");
Label-> settext ("TTT"); // set the text label content to TTT text
If (Label-> text ()! = "My meme ")
{
Emit sigtest ();
}
}
Void mymainwindows: slottest2 () // Signal 2
{
Ledit-> settext ("sigsig"); // set the text box content to sigsig
}
From the code above, we can see that we first added
Copy code
Label = new qlabel ("My meme", this );
Label-> setgeometry (115,10, 100,20 );
The initial content of the label text label is my meme. The position is the form () and the size is (). We added the text label detection in the slot function slottest () described in the previous example, to trigger the custom signal sigtest. As follows:
Copy code
Void mymainwindows: slottest () // signal 1
{
Ledit-> settext ("Jingjing ");
Label-> settext ("TTT"); // set the text label content to TTT text
If (Label-> text ()! = "My meme ")
{
Emit sigtest ();
}
}
Void mymainwindows: slottest2 () // Signal 2
{
Ledit-> settext ("sigsig"); // set the text box content to sigsig
}
As shown in the Code, when the slottest () slot function is called, set the content of the ledit text box to Jingjing, and set the label text label content to TTT, then, we can check whether the label content is still my meme for its initial content. If not, it will trigger our custom sigtest () signal. Pay attention to the emit keywords in the Code. As a QT signal and slot mechanism, emit is used to send signals. That is to say, when we define a signal in the class declaration file, if we want to use this signal, we only need to add the emit keyword before the signal statement.
In the slottest2 () slot function, we set the content of the ledit text box to sigsig. That is, when the slottest2 () slot function is called, the content in ledit is changed to sigsig. In the skill example, we use the sigtest () signal to connect to the slottest2 () slot. That is to say, when the sigtest () signal is triggered, the content of the text box is changed to sigsig. The Code is as follows:
Copy code
Connect (this, signal (sigtest (), this, slot (slottest2 ()));
Now let's put the main. cpp file in the above example together for compilation.
When you click the [modify] button for the first time, the program generates a signal clicked (), which causes the slottest () slot to be executed, and the content of the text box to be modified to Jingjing first, then the label text label content is modified to TTT, and then the slottest () statement finds that the label content is not my meme, so the signal sigtest () is sent outward (). The sigtest () signal is connected to the slottest2 () slot. As a result, the slottest2 () slot function is called and the content of the text box is changed to sigsig. Therefore, when we click Modify, the content of the text box will eventually become sigsig text.