QT Signal and slot connect

Source: Internet
Author: User

QT Signal and slot connect
    • QT Signal and slot connect
      • The Connect function calls several restrictions
      • Connect function code

The function used in the signal-to-slot connection in QT connect is a static function that is defined in the class QObject . It's about the QT5 environment. A more detailed introduction can be seen here http://www.devbean.net/2012/12/how-qt-signals-and-slots-work-qt5/

The Connect function calls several restrictions
  • 1. the sender of the signal must be a QObject derived class object. Otherwise error: invalid conversion from ‘XXX*‘ to ‘const QObject*‘ [-fpermissive]... the error will occur.

  • 2. the object of the callback slot function must be a QObject derived class object. Otherwise error: no matching function for call to ‘QObject::connect(XXX*, the error will occur. One thing to note here is that the signal function, whether present or not, will not error. But if the sender of the signal is NULL , then the problem will be prompted at the time of Operation QObject::connect: Cannot connect (null)::信号函数() to 接收者::槽函数() .

  • For 1,2 two points, the connect strong-turn type can also be compiled at the time of the function call, but the problem will be prompted when running. No such slot QObject::

  • 3. You must add a macro in the definition of the class where the slot function is located Q_OBJECT , or QObject::connect: No such slot ... the error will occur.

    This must be associated with the 2 point, if the added Q_OBJECT macro is not derived from the class QObject , the moc intermediate file will be generated when the error Error: Class contains Q_OBJECT macro but does not inherit from QObject .

    If you Q_OBJECT compile an error after adding a macro, delete the file in undefined reference to ‘vtable for... build the directory Makefile and regenerate it.

  • 4. The modification of the signal function signals and the modification of the slot function slots are in fact necessary. Although the generated intermediate code is removed.

problem cause
error: no matching function for call to ‘QObject::connect(XXX*, The sender and recipient of the signal must be a QObject derived class object.
QObject::connect: No such slot ... After satisfying the condition of a polygon, you must also add (use) the macro in the definition of the class where the slot function is located Q_OBJECT
Error: Class contains Q_OBJECT macro but does not inherit from QObject A class that has added a Q_OBJECT macro is not QObject derived from a
undefined reference to ‘vtable for... This was Q_OBJECT compiled after the macro was added, because the file was not updated Makefile . Please delete build the files in the directory Makefile and use the qmake rebuild.
Connect function code

Connect is a template function, but the type of the parameter is required.
The return value of this function is QMetaObject::Connection of type.

1 //Connect a signal to a pointer to Qobject member function2 //a pointer to a QOBJECT member function that joins a signal3 4Template <typename Func1, TypeName func2>5 Staticinline qmetaobject::connection Connect (6     ConstTypeName Qtprivate::functionpointer<func1>::object *sender,7 FUNC1 Signal,8     ConstTypeName Qtprivate::functionpointer<func2>::object *receiver,9FUNC2 Slot,qt::connectiontype type =qt::autoconnectionTen     ) One { Atypedef qtprivate::functionpointer<func1>Signaltype; -typedef qtprivate::functionpointer<func2>Slottype; -  the     //Check if the signal class has q_object macros -Q_static_assert_x (Qtprivate::hasq_object_macro<typename signaltype::object>:: Value, -                       "No Q_object in the class with the signal"); -  +     //compilation Error If the arguments does not match. -     //If the parameter does not match the compilation error.  +     //A slot function requires more parameters than a signal function.  AQ_static_assert_x (int(Signaltype::argumentcount) >=int(Slottype::argumentcount), at                       "The slot requires more arguments than the signal provides."); -     //the parameters of the signal function and the slot function are not compatible.  -Q_static_assert_x (Qtprivate::checkcompatiblearguments<typename signaltype::arguments, TypeName SlotType:: Arguments>:: Value), -                       "Signal and Slot arguments is not compatible."); -     //The return type of the signal function is the incompatible return type of the slot function.  -Q_static_assert_x (Qtprivate::areargumentscompatible<typename Slottype::returntype, TypeName SignalType:: Returntype>:: Value), in                       "Return type of the slot is a compatible with the return type of the signal."); -  to     Const int*types =0; +     if(type = = Qt::queuedconnection | | type = =qt::blockingqueuedconnection) -types = Qtprivate::connectiontypes<typename signaltype::arguments>:: Types (); the  *     //Qobject::connectimpl is the private intrinsic function used to perform the connection.  $     //a wrapper for the slot function that is used to assist in calling the slot function. Panax Notoginseng     returnConnectimpl (sender, reinterpret_cast<void**> (&signal), -Receiver, reinterpret_cast<void**> (&slot), the                        NewQtprivate::qslotobject<func2, TypeName Qtprivate::list_left<typename signaltype::arguments, SlotType:: Argumentcount>:: Value, +TypeName Signaltype::returntype>(slot), AType, types, &signaltype::object::staticmetaobject); the}

QT Signal and slot connect

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.