Source: QT Custom Function custom signals and slots
in Qt, custom functions, signals, and slots are basically designed to derive from a base class
(Global variables and functions refer to http://blog.csdn.net/liang890319/article/details/7062928)
There are two kinds of situations to discuss here
One, code mode
Pass. H and. CPP design interface layouts, and derive design of custom functions, signals, slots
two, visual design mode
1, design tool design interface
2, the new class inherits the above interface and designs the custom function and signal slot
----------------------------------------------------------------------------------------------------------- -------------
One, code mode
Pass. H and. CPP design interface layouts, and derive design of custom functions, signals, slots
Using custom signals and slots, you need to be aware of the following points:
1. The Declaration and implementation of the class are placed in the. h and. cpp files, respectively;
2, the class declaration contains Q_object macros;
3, the signal as long as the declaration does not design its implementation function;
4, the launch signal with emit keywords;
5. The implementation of custom slots is the same as that of ordinary member functions.
To create a user-defined signal and slot specific steps:
First you need to declare a custom signal and slot in the class declaration. In the keyword public slots: Declare a custom slot, and declare a custom signal under signals: keyword. As shown in the following example:
Class Mymainwindow:public Qwidget//To inherit here, to derive
{
Q_object
Public
void Mymainwindow ();//Self -defining function
void SetValue (int);
Signals:
void valuechanged (int);//Self -defining signal
Public Slots:
void ChangeValue (int);//self-defining slot
};
As you might guess, the SetValue () function should invoke the valuechanged () signal only when a new value is passed to the SetValue () function. Then, by connecting the valuechanged () signal to the ChangeValue () slot, the value can be changed when new values are passed to the SetValue () function. In most cases, this is not necessary, but it demonstrates how the signal is used. The SetValue () function can be implemented like the following format:
void Mymainwindow::setvalue (int value)
{
if (Value!=oldvalue)
{
Oldvalue=value;
Emit valuechanged (value);
}
}
As you can see, the valuechanged () signal is emitted when the new value is different from the old value and the OldValue is modified to value. It should be noted that the signal and trough a class of ordinary functions, it can only use the Emit keyword launch. ChangeValue () can be defined as:
void Mymainwindow::changevalue (int value)
{
Functionforchangingthevalue (value);
}
In this code, call the Functionforchangingthevalue () function to modify the data. The last thing you need to do is connect the signal to the slot:
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 calling the SetValue () function. If unequal, the valuechanged () signal is emitted, and because the valuechanged () signal is connected to the ChangeValue slot, the ChangeValue () slot is executed when the valuechanged () signal is fired. After that, the ChangeValue () slot calls the Functionforchangingthevalue () function to modify the actual data.
(You can also participate in the examination of http://wenku.baidu.com/view/5d0eeffb941ea76e58fa04ff.html
Http://hi.baidu.com/wangfei775/blog/item/be40396f9b5d33d280cb4a5c.html
)
two, visual design mode
1, design tool design interface Ui_xx.h
2, the new class inherits the above interface and designs the custom function and signal slot
xx.h
#include "ui_xx.h"
the sound of a new species,
to inherit the design interface,
and derive a new function signal slot (refer to the code above)
Xx.cpp
In reality