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
#ifndef MYPARENT_H
#define MYPARENT_H
#include
class myParent : public QObject
{
Q_OBJECT
public:
explicit myParent(QObject *parent = nullptr);
signals:
void sig_test();
public slots:
virtual void slot_test();
};
class myChild : public myParent
{
Q_OBJECT
public:
explicit myChild(myParent *parent = nullptr);
void connect_slot();
signals:
void sig_test();
public slots:
void slot_test();
};
#endif // MYPARENT_H
In the first solution, connect is placed in the parent constructor
#include "myparent.h"
#include
myParent::myParent(QObject *parent) : QObject(parent)
{
connect(this, &myParent::sig_test, this, &myParent::slot_test);
}
void myParent::slot_test()
{
qDebug() << "this is parent";
}
myChild::myChild(myParent *parent) : myParent(parent)
{
//connect(this, &myChild::sig_test, this, &myChild::slot_test);
//emit sig_test();
}
void myChild::connect_slot()
{
//connect(this, &myChild::sig_test, this, &myChild::slot_test);
//emit sig_test();
}
void myChild::slot_test()
{
qDebug() << "this is child";
}//Call method
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
The second solution is to use dynamic_ Cast is converted to a subclass object, and then connect is placed in the subclass constructor
#include "myparent.h"
#include
myParent::myParent(QObject *parent) : QObject(parent)
{
//connect(this, &myParent::sig_test, this, &myParent::slot_test);
}
void myParent::slot_test()
{
qDebug() << "this is parent";
}
myChild::myChild(myParent *parent) : myParent(parent)
{
connect(this, &myChild::sig_test, this, &myChild::slot_test);
//emit sig_test();
}
void myChild::connect_slot()
{
//connect(this, &myChild::sig_test, this, &myChild::slot_test);
//emit sig_test();
}
void myChild::slot_test()
{
qDebug() << "this is child";
}//Call method
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();
}
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.
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.