Qt Signal Slot Connection Type

  • Compatibility with earlier versions of Qt 4. The previous release required Qt 4.8. The current version works with Qt 4.6 and up and possibly older releases as well.
  • Compatibility with Qt 5. Though the functionality of QtSignalForwarder can be mostly achieved in Qt 5 using the new signal/slot syntax, this may be useful for creating code which can work with either version or for porting.
  • Performance improvements.
  • QtSignalForwarder::connectWithSender()utility, this provides a convenient way to connect a signal to a slot which includes the sender as the first argument. eg. connectWithSender(button, SIGNAL(clicked()), form, SLOT(buttonClicked(QPushButton*)))
  • signalWhen an object changes its state, the signal is emitted by the object, and the object is only responsible for sending the signal, and it does not know who is receiving the signal on the other end.
  • groove: used to receive signals, and slots are just ordinary object member functions. A slot does not know if there is any signal connected to it.
  • Signal to slot connection: all classes derived from QObject or its subclasses, such as QWidget, can contain signals and slots. It is managed by static method: QObject:: connect (sender, signal (signal), receiver, slot (slot)); where sender and receiver are pointers to objects, and signal() and slot() are macros for converting signals and slots.
  • 1. First of all, we set up a good environment, such as installing qt5.7 (including source code) + vs2013 and corresponding plug-ins on Windows system. We mainly compile and debug through vs.
  • 2. We write a simple instance, build it, and copy the PDB of qtcore in the QT installation directory to our executable file directory, as shown in the following figure:

Step 1: basic structure:

analysis:

Qt Signal Slot Connection Types

The second step is the correlation between signal and slot

  • 1. Check the string of signal and slot first, qsignal_ Code is 1; signal_ Code is 2.
  • 2. Get metadata (the same for sender and receiver).

Qt Signal Slot Connection Type

  • 1) & QWidget:: staticmetaobject → Superdata
  • 2)qt_meta_stringdata_Test.data -> stringdata
  • 3)qt_meta_stringdata_Test() -> data
  • 4)qt_ static_ Metacall (callback function) → static_ metacall
  • 3. Get and save the signal parameters and names. As follows, save the signal parameters and return the method name.
  • 4. Calculate indexes (including base classes).
  • 5. Check the mask.
Qt signal slot connection type
  • 6. Judge the link type. The default is QT:: autoconnection.
  • 1. Autoconnection: automatic connection: by default, the thread and bad object sent by the signal are equivalent to directconnection in one thread, and queuedconnection in different threads.
  • 2. Directconnection: direct connection: it is equivalent to calling the slot function directly. However, when the signaled thread and the slot object are no longer the same thread, the slot function is executed in the issued signal.
  • 3. Queuedconnection: queue connection: implemented internally through postevent. It is not called in real time, and the slot function is always executed in the thread where the slot function object is located. If the signal parameter is a reference type, a separate copy is made. Thread safe.
  • 4. Blockingqueuedconnection: blocking connection: this connection method can only be used for the signaled thread and the object of the slot function. It can be used in a thread any more. It is implemented by semaphore + postevent, and is not called in real time. The slot function is always in the slot The function object is executed in the thread where the function object is located. However, the current thread will be blocked after the execution of the slot function is completed.
  • 5. Uniqueconnection: prevents duplicate connections. If the current signal and slot are already connected, they are no longer connected.
  • 1. Each QObject object has a QObject connectionlistvector structure, which is a vector container. The basic units in it are data of the connectionlist type. The number of connectionlists is the same as the number of signals of the QObject object. Each connectionlist corresponds to a signal, which records all connections connected to the signal. We have seen that there are two important members in the definition of connectionlist: first and last. They are both pointers of connection type, pointing to the first and last connection connected to this signal respectively. All connections connected to this signal are organized in a one-way linked list. The nextconnectionlist member in the connection structure is used to point to the next connection in the list.
  • 2. At the same time, each QObject object also has a senders member. Senders is a pointer of connection type. Senders itself is the head node of a linked list. All nodes in the list are connected to a slot on the QObject object. However, the list mentioned in the following paragraph is not the same, although they may have some common nodes.
  • 3. Each connection object is in two linked lists at the same time. One is a one-way linked list organized by nextconnectionlist members of connection. The common point of each node in this single list is that they all rely on the same signal of the same QObject object. The head node of this list is the first in the connectionlist structure corresponding to the signal; the other is organized by the next and prev members of connection Bidirectional linked list. The common feature of each node in this bi-directional linked list is that their slots are on the same QObject object, and the head node of the list is the sender of the QObject object. The two linked lists will have cross (common nodes), but they have different link pointers, so they are not the same list.
  • 4. In connect, a new connection object is first created. After setting the connection information, it is added to the two linked lists mentioned above. When disconnecting, it is removed from the two linked lists and then deleted. When a QObject object is destroyed, all connections in the bidirectional linked list pointed to by its sender pointer will be removed one by one!

Step 3: send signal to receive signal

  • 1, we click on the button above and then call it into the onDestory slot, which is where we write the signal trigger:
  • 2. The next step is the MOC_ MainWindow.cpp The code inside calls the static method activate of qmetaobject
Connection

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Explain AWS Services in Old-School Term

How to learn Rails the Wyzant way

The magic of Infrastructure As Code

Build a Versioning System With IPFS and Blockstack

Can “dbt” run your Snowpark Python script as a dbt model?

Incident Response vs Incident Management

AWS ECR Creation using Terraform

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ladycasino

ladycasino

More from Medium

CS373 Spring 2022: Sage Sanford

Mr Plow: a Lightweight Logstash Alternative

Comparison among LC low-pass Filter, stepped impedance and Microstrip filter

Complete Overview Of IoT Tech Stack And Its Components — TechSketcher