我正在尝试将一个类中的按钮发送的信号与子类和父类的插槽连接起来。这是一个重现该问题的示例:
错误类.cpp
#include "errorclass.h"
ErrorClass::ErrorClass(QPushButton *button) : QObject()
{
this->button = button;
}
void ErrorClass::makeConnectHappen()
{
connect(button, SIGNAL(pressed()), this, SLOT(exampleSlot()));
}
//SLOT
void ErrorClass::exampleSlot()
{
qDebug() << "ExampleSlot was here";
}
ErrorClassChild.cpp
#include "errorclasschild.h"
ErrorClassChild::ErrorClassChild(QPushButton *button) : ErrorClass(button)
{
makeConnectHappen();
}
void ErrorClassChild::makeConnectHappen()
{
ErrorClass::makeConnectHappen();
connect(button, SIGNAL(released()), this, SLOT(exampleChildSlot()));
}
//SLOT
void ErrorClassChild::exampleChildSlot()
{
qDebug() << "exampleChildSlot was here";
}
最后是带有 QPushButton 的标准 MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "errorclasschild.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ErrorClassChild ecc(ui->pushButton);
}
MainWindow::~MainWindow()
{
delete ui;
}
ErrorClass.hmakeConnectHappen()
中的虚函数是由 ErrorClassChild 继承和扩展的。我希望这会很清楚。
当我编译并运行程序时,有应用消息
QObject::connect: No such slot ErrorClass::exampleChildSlot() in ../QListWidgetProblem/errorclasschild.cpp:11
QObject::connect: (sender name: 'pushButton')
现在,当我将exampleChildSlot()
父级作为纯虚拟插槽放入时,错误消失了,但没有qDebug()
显示任何消息。
如何同时制作connect
与父母和孩子的插槽?还是我的想法完全错误?