Several common forms of signal slot connection and disconnection

connect

1. Use the form of SIGNAL() and SLOT() macros:

[static] QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)

1. Create a connection of the given type from the signal in the sender object to the method in the receiver object. Returns the handle of the connection, which can be used to disconnect later.

2. When specifying signals and methods, SIGNAL () and SLOT () must be used , for example:

  QLabel *label = new QLabel;
  QScrollBar *scrollBar = new QScrollBar;
  QObject::connect(scrollBar, SIGNAL(valueChanged(int)),label,  SLOT(setNum(int)));

3. The signal and slots parameters cannot contain any variable names, only types.

4. One signal can also be connected to another signal.

5. One signal can be connected to multiple slots and signals. Multiple signals can be connected to a slot.

6. If the signal is connected to multiple slots, when the signal is sent, the activation sequence of the slots is the same as the connection sequence.

7. If the signal is successfully connected to the slot, the function returns a QMetaObject::Connection, which represents the handle of the connection. If the connection handle cannot create a connection, for example, if QMetaObject cannot verify the existence of a signal or method, the connection handle will be invalid.

8. By default, each connection sends out one signal, and repeated connections will send out two signals. All repeated connections can be disconnected by a single disconnect() call. If the Qt::UniqueConnection type is passed , the connection will only be made when the connection is not repeated. If there are already duplicates (the signals of the same slot on the same object are exactly the same), the connection will fail and connect will return an invalid QMetaObject::Connection.

Note: Qt::UniqueConnections does not apply to lambdas, non-member functions and function objects . Applies only to connection to member functions.

9. Parameter 5 describes the type of connection to be established. In particular, it determines whether a specific signal is sent to the slot immediately or is waiting in a queue for processing. If the signal is queued, the parameters must be of a type known to the Qt meta-object system, because Qt needs to copy the parameters to store them in the queue event. If you try to use a queued connection and get the following error message:

QObject::connect: Cannot queue arguments of type 'MyType'(Make sure 'MyType' is registered using qRegisterMetaType().)

 Before establishing the connection, call qRegisterMetaType () to register the data type.

Second, the form of function pointer

[static] QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)

1. The signal must be a function declared as a signal in the header. The slot function can be any member function that can be connected to a signal.

2. If the parameters of the signal are at least the same as the number of parameters of the slot, and the type of the corresponding parameter in the signal and the corresponding parameter of the slot function can be implicitly converted.

Three, non-member slot function form

[static] QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)

1. The function can be a lambda expression or a non-member function.

2. The parameter requirements of the signal slot are the same as above.

3. If the signal sender is deleted, the connection becomes invalid.

disconnect

Disconnect If the connection is successfully disconnected, it returns true; otherwise, it returns false. When the receiver or sender is deleted, the signal slot connection will be deleted.
disconnect() usually has four uses:

1、disconnect(myObject, nullptr, nullptr, nullptr);

Disconnect all connections to the target signal. Equivalent to: myObject->disconnect();

2、disconnect(myObject, SIGNAL(mySignal()), nullptr, nullptr);

Disconnect all connections for a specific signal. Equivalent to: myObject->disconnect(SIGNAL( mySignal ()));

3、disconnect(myObject, nullptr, myReceiver, nullptr);

Disconnect all connections with a specific object, which is equivalent to: myObject->disconnect(myReceiver);

4、disconnect(lineEdit, &QLineEdit::textChanged,label,  &QLabel::setText);

Disconnect a specific connection with a specific object (only for the connection of the function pointer type ), equivalent to: myObject->disconnect(lineEdit, &QLineEdit:: textChanged ,label,  nullptr );

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/114747845