QT Signal Slot Connection--based on the difference between a string and a function-based connection (translation)

Source: Internet
Author: User

Starting with Qt5.0, QT provides two different ways to connect a signal slot: string-based connection syntax, function-based connection syntax. Both of these connection syntaxes have pros and cons, and the table below summarizes their differences.

The following sections explain the differences between them in detail, and explain how each of these connection syntaxes uses its advantages.

Type checking and implicit type conversions

String-based connections are type-checked at run time through string comparisons, with 3 limitations:

1. Connection errors can only be detected after the program is run;

2. There is no implicit conversion between the signal and the slot;

3. Type definitions and namespaces cannot be identified.

The 2nd 3rd limitation exists because the comparison of strings does not involve the type information of C + +, so it relies strictly on the matching of strings.

Instead, a function-based connection is checked by the compiler, which catches errors at compile time and allows implicit conversions of compatible types, and it can recognize the same type of name that cannot be.

For example, the signal has an int type parameter, and the slot accepts a double type of argument, and only the function-based syntax can connect them together. Qslider has an int value, and Qdublespinbox has a double value, the following code snippet shows how to keep the two synchronized:

Auto slider = new Qslider (this), auto Doublespinbox = new Qdoublespinbox (this);//ok:the compiler can convert an int into A doubleconnect (slider, &qslider::valuechanged,doublespinbox, &qdoublespinbox::setvalue);//Error:the string table doesn ' t contain conversion informationconnect (slider, SIGNAL (int)), valuechanged, SLOT ( SetValue (double));

The following example illustrates the lack of name recognition.

Qaudioinput::statechanged () declaration with a "Qaudio

: "state" type of parameter, therefore, string-based connections must also recognize "qaudio::satae" even if "state" is already visible. This problem does not apply to function-based connections because the parameter type is not part of the connection.

Auto Audioinput = new Qaudioinput (Qaudioformat (), this), Auto widget = new Qwidget (this);//Okconnect (Audioinput, SIGNAL (s Tatechanged (qaudio::state)), Widget, SLOT (Show ())),//Error:the strings "state" and "Qaudio::state" Don t matchusing Namespace Qaudio;connect (Audioinput, SIGNAL (state), Widget, SLOT (Show ())),//...
Connect using a lambda expression

The function-based connection syntax connects the signal to a lambda expression in the c++11 (which is actually an inline slot function) that does not apply to string-based syntax.

In the following example, the class Textsender emits a signal textcompleted (), which has a parameter of type qstring, and the following is the definition of this class:

Class Textsender:public Qwidget {Q_objectqlineedit *lineedit; Qpushbutton *button;signals:void textcompleted (const qstring& text) Const;public:textsender (QWidget *parent = nullptr);};

Below is the signal slot connection, which signals when the button is clicked Textsender::textcompleted ():

Textsender::textsender (Qwidget *parent): Qwidget (parent) {LineEdit = new Qlineedit (this); button = new Qpushbutton ("Send ", this); Connect (button, &qpushbutton::clicked, [=] {emit textcompleted (Lineedit->text ());}); / ...}

In this example, the lambda function makes the connection very simple, even if qpushbutton::clicked () and textsender::textcompleted () have mismatched parameters. Conversely, if you use string-based implementations, you need additional code.

Note: The function-based connection syntax accepts pointers to all functions, including normal non-member functions and member functions, but for readability, the signal should only be connected to slots, lambda expressions, and other signals.

Connecting C + + objects and QML objects

String-based syntax can connect C + + objects and QML objects, but function-based syntax cannot be connected. Because the QML type is recognized at run time, they do not apply to the C + + compiler.

Use default parameters in slots to connect to signals with fewer parameters

Typically, a connection can be established only if the parameter of the slot is less than the parameter equal to the signal and all parameters match.

The string-based connection syntax provides a workaround for this rule: when the signal is emitted and its parameters are less than the parameters of the slot, if the slot has default parameters, these parameters can be omitted from the signal, and QT uses the default value to call the slot function.

Function-based connections do not support this feature.

If there is a class called Demowidget, the slot function Printnumber () has a default parameter:

Public slots:void printnumber (int number = $) {qdebug () << "Lucky number" << number;}

With string-based joins, Demowidget::p rintnumber () can be connected to qapplication::abouttoquit (), even if the latter has fewer parameters. The function-based connection will be wrong at compile times:

Demowidget::D emowidget (Qwidget *parent): Qwidget (parent) {//Ok:printnumber () 'll be called with a default value of 42c Onnect (Qapp, SIGNAL (Abouttoquit ()), this, SLOT (Printnumber ()));//Error:compiler requires compatible argumentsconnect (Qapp, &qapplication::abouttoquit,this, &demowidget::p rintnumber);}
Select an overloaded signal or slot

With string-based syntax, the parameter type is explicitly determined. Finally, the use of which overload signal or slot is ambiguous.

Conversely, with function-based syntax, overloaded signals or slots must be cast to tell the compiler which to use.

For example, Qsignalmapper has 4 types of mapped () signals:

1.qsignalmapper::mapped (int)
2.qsignalmapper::mapped (QString)
3.qsignalmapper::mapped (qwidget*)
4.qsignalmapper::mapped (qobject*)

In order to connect the int version of Qspinbox::setvalue (), the two syntaxes are as follows:

Auto Mapper = new Qsignalmapper (this), auto Spinbox = new Qspinbox (this);//string-based Syntaxconnect (Mapper, SIGNAL (MAPP Ed (int)), Spinbox, SLOT (setValue (int)));//functor-based syntax, first alternativeconnect (Mapper, static_cast<void (Qsignalmapper::*) (int) > (&qsignalmapper::mapped), Spinbox, &qspinbox::setvalue);//functor-based syntax, second Alternativevoid (qsignalmapper::* mysignal) (int) = &qsignalmapper::mapped;connect (Mapper, Mysignal,spinbox, &qspinbox::setvalue);

QT Signal Slot Connection--based on the difference between a string and a function-based connection (translation)

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.