4/3/2022
75
Qt Signal Slot Custom Class Rating: 5,6/10 8335 reviews

QtCore.SIGNAL and QtCore.SLOT macros allow Python to interface with Qt signal and slot delivery mechanisms. This is the old way of using signals and slots. The example below uses the well known clicked signal from a QPushButton. The connect method has a non python-friendly syntax.

  • The connection mechanism uses a vector indexed by signals. But all the slots waste space in the vector and there are usually more slots than signals in an object. So from Qt 4.6, a new internal signal index which only includes the signal index is used. While developing with Qt, you only need to know about the absolute method index.
  • The class in Qt responsible for custom types is QMetaType. To make the type known to this class, we invoke the QDECLAREMETATYPE macro on the class in the header file where it is defined.
  • To allow for these slots and signals to be defined within the class you need to call the macro QOBJECT at the top of the class (within a private area) so that all of the necessary attachments to the class can be created (within the moc file associated with the class when you run the qmake later on).

Contents:

Overview

Qt provides a range of standard value types that are used to provide rich and meaningful APIs. These types are integrated with the meta-object system, enabling them to be stored in QVariant objects, written out in debugging information and sent between components in signal-slot communication.

Custom types can also be integrated with the meta-object system as long as they are written to conform to some simple guidelines. In this example, we introduce a simple Message class, we describe how we make it work with QVariant, and we show how it can be extended to generate a printable representation of itself for use in debugging output.

The Message Class Definition

The Message class is a simple value class that contains two pieces of information (a QString and a QStringList), each of which can be read using trivial getter functions:

The default constructor, copy constructor and destructor are all required, and must be public, if the type is to be integrated into the meta-object system. Other than this, we are free to implement whatever we need to make the type do what we want, so we also include a constructor that lets us set the type's data members.

To enable the type to be used with QVariant, we declare it using the Q_DECLARE_METATYPE() macro:

We do not need to write any additional code to accompany this macro.

To allow us to see a readable description of each Message object when it is sent to the debug output stream, we define a streaming operator:

This facility is useful if you need to insert tracing statements in your code for debugging purposes.

The Message Class Implementation

The implementation of the default constructor, copy constructor and destructor are straightforward for the Message class:

The streaming operator is implemented in the following way:

Here, we want to represent each value depending on how many lines are stored in the message body. We stream text to the QDebug object passed to the operator and return the QDebug object obtained from its maybeSpace() member function; this is described in more detail in the Creating Custom Qt Types document.

Qt signals and slots tutorial

We include the code for the getter functions for completeness:

With the type fully defined, implemented, and integrated with the meta-object system, we can now use it.

Using the Message

In the example's main() function, we show how a Message object can be printed to the console by sending it to the debug stream:

You can use the type with QVariant in exactly the same way as you would use standard Qt value types. Here's how to store a value using the QVariant::setValue() function:

Alternatively, the QVariant::fromValue() and qVariantSetValue() functions can be used if you are using a compiler without support for member template functions.

The value can be retrieved using the QVariant::value() member template function:

Alternatively, the qVariantValue() template function can be used if you are using a compiler without support for member template functions.

Further Reading

The custom Message type can also be used with direct signal-slot connections.

To register a custom type for use with queued signals and slots, such as those used in cross-thread communication, see the Queued Custom Type Example.

Qt Signal Slot Custom Classic

Qt Signal Slot Custom ClassQt Signal Slot Custom Class

More information on using custom types with Qt can be found in the Creating Custom Qt Types document.

Files:

© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

Background:I defined a QT parent class, which has a custom signal and slot function. I defined the parent class pointer in the main function to point to the subclass object. At this time, I sent a signal, and the subclass could not receive it. We started our exploration…

Solution:According to the breakpoint, it is found that the signal of the parent class is called, but the signal slot is associated with the subclass constructor. At this time, it is OK to put connect in the parent class (this is just a solution); the other is to view the parent class calling the subclass method dynamic on the Internet_ Cast, let’s explain my two solutions with code

The code says:

1、 Let’s first look at the header files of the parent and child classes

In the first solution, connect is placed in the parent constructor

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//std::unique_ptr test = std::unique_ptr(new myChild);
//dynamic_cast(test.get())->connect_slot();
//test->connect_slot();
myParent *test = new myChild;
emit test->sig_test();
//emit dynamic_cast(test)->sig_test();
}

This is a child

Qt Signal Slot Example

The second solution is to use dynamic_ Cast is converted to a subclass object, and then connect is placed in the subclass constructor

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
std::unique_ptr test = std::unique_ptr(new myChild);
//dynamic_cast(test.get())->connect_slot();
//test->connect_slot();
//myParent *test = new myChild;
//emit test->sig_test();
emit dynamic_cast(test.get())->sig_test();
}

Qt Signal And Slots

This is the problem encountered in the development, to share with you, if there is any better solution or such problems, you are welcome to share and comment.

Qt Signals And Slots Tutorial

Question: can’t QT signal be polymorphic? Signal of the same name (whether the signal subclass of the parent class is inherited)? Because according to my solution, I still have some doubts… Please answer.