[英]QObject::connect: signal not found (in subclass)
我設法將我的應用程序的問題歸結為一個小樣本。
我無法連接在基礎 class 中定義的信號。
這是我的簡單 wtf.pro 文件:
QT += widgets
SOURCES = wtf.cpp TableViewSubclass.cpp
HEADERS = TableViewSubclass.h
這是我的 TableViewSubclass.h:
#include <QTableView>
class TableViewSubclass : public QTableView
{
Q_OBJECT
public:
TableViewSubclass(QWidget *parent = nullptr)
: QTableView(parent) {}
virtual ~TableViewSubclass() {}
void setup();
private slots:
void viewColumnCountChanged(int oldcount, int newcount);
};
這是我的 TableViewSubclass.cpp。 為了更好地衡量,我嘗試了兩種將信號連接到插槽的方法。
#include "TableViewSubclass.h"
void
TableViewSubclass::setup()
{
// Trying to use &QTableView::columnCountChanged for parameter 2
// gives me a compiler error, for being protected within this context.
QObject::connect(this, &TableViewSubclass::columnCountChanged,
this, &TableViewSubclass::viewColumnCountChanged);
QObject::connect(this, SIGNAL(columnCountChanged(int,int)),
this, SLOT(viewColumnCountChanged(int,int)));
}
void
TableViewSubclass::viewColumnCountChanged(int /* oldcount */, int /* newcount */)
{
// TODO
}
最后,主要的 wtf.cpp:
#include <QApplication>
#include "TableViewSubclass.h"
int
main(int argc, char **argv)
{
QApplication app(argc, argv);
TableViewSubclass oView;
oView.setup();
}
我使用qmake-qt5 wtf.pro
和make
,然后使用./wtf
運行。 它輸出:
QObject::connect: signal not found in TableViewSubclass
QObject::connect: No such signal TableViewSubclass::columnCountChanged(int,int)
該信號在QTableView
中定義,它是TableViewSubclass
的基礎 class 。 怎么找不到? 好像我錯過了一些明顯和愚蠢的東西。
上面的示例在 Fedora Core 35 和 Qt 5.15.2 下運行,但在 MinGW 中首次遇到,也是在 Qt 5.15.2 下運行的。
根據Qt 文檔, countColumnChanged(int, int)
是一個槽而不是一個信號。 您可以在派生的 class 中覆蓋此受保護的插槽:
#include <QTableView>
class TableViewSubclass : public QTableView
{
Q_OBJECT
public:
TableViewSubclass(QWidget *parent = nullptr)
: QTableView(parent) {}
virtual ~TableViewSubclass() {}
void setup();
protected slots:
void columnCountChanged(int oldcount, int newcount) override;
};
每次列數更改時,基本 class (QTableView) 都會調用該插槽。 因此,無需將任何信號連接到此插槽來更新派生的 class 中的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.