[英]Qt won't connect to a slot in a subclass
I have subclassed QTabWidget and added a new public slot.我已经继承了 QTabWidget 并添加了一个新的公共插槽。 When I try to connect to that slot, Qt tells me that my slot doesn't exist.
当我尝试连接到该插槽时,Qt 告诉我我的插槽不存在。 But seems to be looking in the parent class.
但似乎正在寻找父类。 What am I missing?
我错过了什么?
Here is a minimal program that reproduces the problem.这是一个重现问题的最小程序。 I am running it in Qt 5.13 with a MinGW 32-bit package.
我在 Qt 5.13 中使用 MinGW 32 位包运行它。
#include <QMainWindow>
#include <QApplication>
#include <QTabWidget>
#include <QTabBar>
#include <QDebug>
class MyTabWidget : public QTabWidget
{
public:
MyTabWidget(QWidget *parent) : QTabWidget(parent)
{
qDebug() << "connect() returns " <<
connect(this->tabBar(),SIGNAL(tabBarDoubleClicked(int)),this,SLOT(changeTabName(int)));
}
public slots:
void changeTabName(int index)
{
tabBar()->setTabText(index,"New Name");
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mw = new QMainWindow();
QWidget *tw;
MyTabWidget *t = new MyTabWidget(mw);
tw = new QWidget(mw);
t->addTab(tw,"Double-click This Tab");
mw->setCentralWidget(t);
mw->show();
return a.exec();
}
Here is the output.这是输出。 When connect() is called, "this" is a pointer to the subclass, right?
当 connect() 被调用时,“this”是一个指向子类的指针,对吗? So why is it looking for the slot in the superclass?
那么它为什么要在超类中寻找插槽呢?
QObject::connect: No such slot QTabWidget::changeTabName(int) in main.cpp:14
QObject::connect: (sender name: 'qt_tabwidget_tabbar')
connect() returns false
You have the following errors:您有以下错误:
If a class that inherits from QObject like all widgets is going to have signals, slots, etc. then you must use the Q_OBJECT macro to create the necessary implementations associated with those elements.如果像所有小部件一样从 QObject 继承的类将具有信号、槽等,那么您必须使用 Q_OBJECT 宏来创建与这些元素关联的必要实现。 Therefore, if you are using Q_OBJECT in the main file (filename.cpp) then you must include "filename.moc" to include the above.
因此,如果您在主文件 (filename.cpp) 中使用 Q_OBJECT,那么您必须包含“filename.moc”以包含上述内容。
You must use the new connection syntax, although it is not an error but can be a source of silent bugs.您必须使用新的连接语法,虽然它不是错误但可能是静默错误的来源。
Considering the above, the solution is:综上所述,解决方法是:
main.cpp主程序
#include <QMainWindow>
#include <QApplication>
#include <QTabWidget>
#include <QTabBar>
#include <QDebug>
class MyTabWidget : public QTabWidget
{
Q_OBJECT
public:
MyTabWidget(QWidget *parent) : QTabWidget(parent)
{
qDebug() << "connect() returns " <<
connect(tabBar(), &QTabBar::tabBarDoubleClicked, this, &MyTabWidget::changeTabName);
}
public slots:
void changeTabName(int index)
{
tabBar()->setTabText(index,"New Name");
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mw = new QMainWindow();
QWidget *tw;
MyTabWidget *t = new MyTabWidget(mw);
tw = new QWidget(mw);
t->addTab(tw,"Double-click This Tab");
mw->setCentralWidget(t);
mw->show();
return a.exec();
}
#include "main.moc"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.