Signals and slots

Source: Internet
Author: User

1) The definition of the signal must be in the signals: reserved word, and do not need to implement

2) The definition of the slot must be in slots: reserved word, need to implement

3) signal and slot are connected by Qobject::connect function

4) When the signal is triggered, the slot function is called


It is important to note that:

1) signal and groove, is the extension of QT, so realize the signal and slot class, must be Qobject subclass

2) to implement the signal and slot classes, you must start with the macro Q_object

3) Connect signals and slots, use signal and slot macros, convert function to string

4) A signal can be connected with multiple slots, and the order of the slot function calls is indeterminate

5) Multiple signals can be connected to one slot at a time

6) signal can be connected to signal, forming signal conduction

7) When the signal is the same as the number of parameters of the slot function, the parameter types are exactly the same.

When the parameters of the signal and the number of parameters of the slot function are not the same, only the number of parameters of the signal is more than the number of parameters of the slot function, and the same number of parameters should be consistent before the signal, the excess parameters are ignored.

8) both signal and slot can be overloaded

9) Both the signal and the slot can have default parameters

10) Slot functions can be called as normal functions

11) in the slot function, call sender to get the signal caller

Summary below:

A class: Qobject signals and slots are subclasses of this class

Three macros: Q_object SIGNAL SLOT

Three reserved words: Signals, slots, emit

In Qt 5, QObject::connect() There are five overloads:

1 //First, the sender type is const QOBJECT *,signal type is const char *,receiver type is const qobject *,slot type is const char *. This function handles signal and slots as strings2Qmetaobject::connection Connect (ConstQobject *,Const Char*,3                                 ConstQobject *,Const Char*,4 qt::connectiontype);5 6 //Second, sender and receiver are also const qobject *, but signal and slots are const Qmetamethod &7Qmetaobject::connection Connect (ConstQobject *,ConstQmetamethod &,8                                 ConstQobject *,ConstQmetamethod &,9 qt::connectiontype);Ten  One //Third, sender is also const Qobject *,signal and slot are also const char *, but receiver is missing. This function actually takes the this pointer as receiver AQmetaobject::connection Connect (ConstQobject *,Const Char*, -                                 Const Char*, -Qt::connectiontype)Const; the  - //fourth, both sender and receiver exist, both const qobject *, but signal and slot types are pointertomemberfunction. Look at this name and you should know that this is a pointer to the member function -Qmetaobject::connection Connect (ConstQobject *, Pointertomemberfunction, -                                 ConstQobject *, Pointertomemberfunction, + Qt::connectiontype) -  + //Fifth, the first two parameters are not different, the last parameter is the functor type. This type can accept static functions, global functions, and LAMBDA expressions AQmetaobject::connection Connect (ConstQobject *, Pointertomemberfunction, atFunctor);

Note: Use lambda expressions in. Pro to add CONFIG + = c++11

QT4 we used the two macros, signal and slots, to convert two function names into strings. Note that even if quit () is a static function of qapplication, you must also pass in an object pointer. This is also the limitation of the signal slot syntax for Qt 4. Also, notice that the signal and slots of the Connect () function accept strings, so you cannot pass in a global function or LAMBDA expression to connect (). In the event that the connection is unsuccessful, Qt 4 is not compiled (because everything is a string, the compile time does not check whether the string matches), but instead gives an error at run time. This will undoubtedly increase the instability of the program.

eg

1#include <QApplication>2#include <QWidget>3#include <QHBoxLayout>4#include <QSlider>5#include <QSpinBox>6 7 8 intMainintargcChar**argv)9 {Ten qapplication app (ARGC,ARGV); One  A Qwidget W; -W.setwindowtitle ("Enter your age"); -  theQspinbox *spinbox =NewQspinbox (&W); -Qslider *slider =NewQslider (qt::horizontal,&W); -Spinbox->setrange (0, the); -Slider->setrange (0, the); +  -     /* + //qt4, using signal and slots, A Qobject::connect (slider,signal (valuechanged (int)), Spinbox,slot (setValue (int) )); at Qobject::connect (spinbox,signal (valuechanged (int)), Slider,slot (setValue (int) )); -     */ -  -     //The notation of QT5, with pointers to member functions -Qobject::connect (slider,&qslider::valuechanged,spinbox,&qspinbox::setvalue); -  in     //If you do not add the following line, you will get an error because Qspinbox does have two signals: void valuechanged (int) and void valuechanged (const QString &) -     //using Qt 4 's signal and slot macros, because the two macros already have parameter information specified, there is no problem, and the workaround uses a function pointer to explicitly specify which signal to use to     void(Qspinbox:: *spinboxsignal) (int) = &qspinbox::valuechanged; +Qobject::connect (spinbox,spinboxsignal,slider,&qslider::setvalue); -  the  *Spinbox->setvalue ( -); $ Panax NotoginsengQhboxlayout *layout =Newqhboxlayout; -Layout->AddWidget (spinbox); theLayout->addwidget (slider); + w.setlayout (layout); A w.show (); the  +     returnapp.exec (); -}

Signals and slots

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.