QTimer::singleShot() 在给定对象的父类中查找指定的槽,而不是对象本身

发布于 2024-12-21 17:00:23 字数 1246 浏览 2 评论 0原文

我对 Qt 还很陌生。我已经对现有的 Qt 应用程序做了一些简单的修改,但我还没有从头开始创建任何应用程序。
一般来说,我对 C++ 的某些方面(类继承等)也没有太多经验。

我创建了一个新的基于 Qt4 的 Code::Blocks 项目并对模板进行了一些修改。我添加了两个文件。
现在该项目包含三个文件:main.cpp、app.h 和 app.cpp。
这是 ma​​in.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-12-28 17:00:23

您只是在 TestApp 类中缺少 Q_OBJECT 宏:

class TestApp: public QApplication {
    Q_OBJECT

    public:
    ...

这是整个信号/槽基础结构正常工作所必需的(并且从具有此宏的类派生是不够的)。

(确保在更改后进行完整、干净的构建 - 如果您不使用 qmake 或其他一些支持 Qt 的构建系统,则需要运行 moc代码> 自己。)

有关参考,请参阅 QObject文档:

请注意,Q_OBJECT 宏对于任何实现信号、槽或属性的对象都是必需的。您还需要在源文件上运行元对象编译器。我们强烈建议在 QObject 的所有子类中使用此宏,无论它们是否实际使用信号、槽和属性,因为不这样做可能会导致某些函数表现出奇怪的行为。

You're simply missing the Q_OBJECT macro in your TestApp class:

class TestApp: public QApplication {
    Q_OBJECT

    public:
    ...

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 run moc yourself.)

For reference, see the QObject docs:

Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.

生生漫 2024-12-28 17:00:23

您需要创建一个 moc 文件,如果您将 Q_OBJECT 宏放入类中,则使用 qmake 创建该文件。

因此,要修复您的示例,您需要将您的类更改为:

class TestApp: public QApplication {
    Q_OBJECT
    public:
    TestApp(int &argc, char **argv);
    public slots:
    void timeout();
};

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 :

class TestApp: public QApplication {
    Q_OBJECT
    public:
    TestApp(int &argc, char **argv);
    public slots:
    void timeout();
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文