QTimer::singleShot() 在给定对象的父类中查找指定的槽,而不是对象本身
我对 Qt 还很陌生。我已经对现有的 Qt 应用程序做了一些简单的修改,但我还没有从头开始创建任何应用程序。
一般来说,我对 C++ 的某些方面(类继承等)也没有太多经验。
我创建了一个新的基于 Qt4 的 Code::Blocks 项目并对模板进行了一些修改。我添加了两个文件。
现在该项目包含三个文件:main.cpp、app.h 和 app.cpp。
这是 main.cpp 的内容:
#include <QTimer>
#include "app.h"
int main(int argc, char* argv[]) {
TestApp app(argc, argv);
QTimer::singleShot(1000, &app, SLOT(timeout()));
return app.exec();
}
这是 app.h 的样子:
#ifndef APP_H_INCLUDED
#define APP_H_INCLUDED
#include <QApplication>
class TestApp: public QApplication {
public:
TestApp(int &argc, char **argv);
public slots:
void timeout();
};
#endif
这是 app.cpp:
#include "app.h"
#include <QDebug>
TestApp::TestApp(int &argc, char **argv): QApplication(argc, argv) {
}
void TestApp::timeout() {
qDebug() << "timeout called";
}
我期望这个程序启动后一秒打印“超时已调用”。不幸的是,这不起作用。当QTimer::singleShot()
被调用时,控制台显示:
Object::connect: No such slot QApplication::timeout() in [path to the main.cpp file]
Object::connect: (receiver name: 'QtTests')
我不知道如何处理这个问题。
I am fairly new to Qt. I have done some simple modifications to an existing Qt application, but I haven't created any from scratch yet.
I also don't have really much experience with certain aspects of C++ in general (class inheritance etc).
I have created a new Code::Blocks Qt4-based project and modified the template a bit. I have added two files.
Right now the project contains three files: main.cpp, app.h and app.cpp.
This is the content of main.cpp:
#include <QTimer>
#include "app.h"
int main(int argc, char* argv[]) {
TestApp app(argc, argv);
QTimer::singleShot(1000, &app, SLOT(timeout()));
return app.exec();
}
This is what app.h looks like:
#ifndef APP_H_INCLUDED
#define APP_H_INCLUDED
#include <QApplication>
class TestApp: public QApplication {
public:
TestApp(int &argc, char **argv);
public slots:
void timeout();
};
#endif
And this is app.cpp:
#include "app.h"
#include <QDebug>
TestApp::TestApp(int &argc, char **argv): QApplication(argc, argv) {
}
void TestApp::timeout() {
qDebug() << "timeout called";
}
I expected the program to print "timeout called" one second after startup. Unfortunately, this doesn't work. When QTimer::singleShot()
is called, the console says:
Object::connect: No such slot QApplication::timeout() in [path to the main.cpp file]
Object::connect: (receiver name: 'QtTests')
I have no idea how to deal with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只是在 TestApp 类中缺少
Q_OBJECT
宏:这是整个信号/槽基础结构正常工作所必需的(并且从具有此宏的类派生是不够的)。
(确保在更改后进行完整、干净的构建 - 如果您不使用 qmake 或其他一些支持 Qt 的构建系统,则需要运行 moc代码> 自己。)
有关参考,请参阅
QObject
文档:You're simply missing the
Q_OBJECT
macro in your TestApp class:This is necessary for the whole signal/slot infrastructure to work (and deriving from a class that has this macro is not sufficient).
(Make sure you do a full, clean build after that change - and if you don't use
qmake
or some other Qt-aware build system, you'll need to runmoc
yourself.)For reference, see the
QObject
docs:您需要创建一个 moc 文件,如果您将 Q_OBJECT 宏放入类中,则使用 qmake 创建该文件。
因此,要修复您的示例,您需要将您的类更改为:
You need to create a moc file, which is created with qmake if you put Q_OBJECT macro in your class.
So, to fix your example, you need your class changed to this :