Qt citic and slot are light and light

Source: Internet
Author: User

All classes derived from QObject or its subclass (such as Qwidget) can containSignalAndSlot. When an object changes its status,SignalThis is all the things the object has to do. It does not know who is receivingSignal. This is the true information encapsulation, which ensures that the object is used as a real software component.SlotFor receivingSignalBut they are common object member functions. OneSlotI don't know if there is anySignalConnect to yourself. Moreover, the object does not understand the specific communication mechanism.

Use customSignalAndSlot, Pay attention to the following points:

1. The class declaration and implementation are placed in the. h and. cpp files respectively;

2. The class Declaration contains the Q_OBJECT macro;

3,SignalAs long as the Declaration does not design its implementation functions;

4. Use the emit keyword to transmit signals;

5. The implementation of custom slots is the same as that of common member functions.

When the classes used in the program have user-defined slots and signals, you need to use moc (metadatabase compiler) to compile the program.

Compilation Method 1:

You can compile the moc file as the target file, and then link it with ClassDefinion. cpp and main. cpp. The specific operation steps are as follows:

 
 
  1. #moc ClassDeclaration.h -o ClassDeclaration.cpp   
  2. #g++ -c ClassDeclaration.cpp -o ClassDeclaration.o   
  3. #g++ -c ClassDefinion.cpp -o ClassDefinion.o   
  4. #g++ -c main.cpp -o main.o   
  5. #g++ -lqt ClassDefinion.o ClassDeclaration.o main.o -o MyProgram  

Compilation Method 2:

Create an empty folder, put the source file ClassDeclaration. h, ClassDefinion. cpp and main. cpp into it, and execute the following command:

 
 
  1. #qmake -project   
  2. #qmake   
  3. #make  

After completing the preceding steps, you can generate an executable file. The executable file name is the same as the name of the folder you created.) Then you can run the file.

To create a custom signal and slot, follow these steps:

First, declare the custom signal and slot in the class declaration. Declare the custom slots under the public slots keyword; declare the custom signal under the signals: keyword. For example:

 
 
  1. class MyMainWindow : public QWidget   
  2. {   
  3. Q_OBJECT   
  4. public:   
  5. void MyMainWindow();   
  6. void SetValue(int);   
  7. signals:   
  8. void ValueChanged(int);   
  9. public slots:   
  10. void ChangeValue(int);   
  11. };  

As you may guess, the SetValue () function should call the ValueChanged () signal only when a new value is passed to the SetValue () function. Then, the ValueChanged () signal is connected to the ChangeValue () slot, so that when a new value is passed to the SetValue () function, the value changes. In most cases, this is unnecessary, but it demonstrates how to use the signal. The SetValue () function can be implemented in the following format:

 
 
  1. void MyMainWindow::SetValue(int value)   
  2. {   
  3. if(value!=oldvalue)   
  4. {   
  5. oldvalue=value;   
  6. emit ValueChanged(value);   
  7. }   
  8. }  

As you can see, ValueChanged () is only sent when the new value and the old value are different ()SignalAnd the oldvalue will be changed to value. It should be noted that,SignalAndSlotA Class of common functions are different. They can only be transmitted using the emit keyword. ChangeValue () can be defined:

 
 
  1. void MyMainWindow::ChangeValue(int value)   
  2. {   
  3. FunctionForChangingTheValue(value);   
  4. }  

In this Code, call the FunctionForChangingTheValue () function to modify the data. The last thing you need to do isSignalAndSlotConnect:

 
 
  1. connect(this,SIGNAL(ValueChanged(int)),this,SLOT(ChangeValue(int)));  

The function of this example is to check whether the new value is equal to the old value when the SetValue () function is called. If not, ValueChanged () is initiated ()Signal, And because ValueChanged ()SignalConnected to ChangeValueSlotTherefore, when ValueChanged () is initiated ()SignalChangeValue () will be executed ()Slot. Then, ChangeValue ()SlotCall the FunctionForChangingTheValue () function to modify the actual data.

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.