简体   繁体   English

QObject :: connect()错误

[英]Error with QObject::connect()

I am trying to run QTimer and have it warn me when timeout ing. 我正在尝试运行QTimer并在timeout时警告我。 To do so, I use slot and signal to link the two. 为此,我使用slotsignal来链接两者。

The guy.h : guy.h

#ifndef GUY_H
#define GUY_H

#include <QGraphicsItem>
#include <QTimer>
#include <QObject>

class Guy : public QGraphicsItem
{
public:
    Guy(int x, int y);

    void timerStart();

public slots:
    void onTimeOutTimer();

    [...]

    QTimer *timer;
}

#endif // GUY_H

The guy.cpp : guy.cpp

#include "guy.h"

#include <QTimer>
#include <QObject>

#include <stdio.h>
#include <iostream>

Guy::Guy(int x, int y)
{
    timer = new QTimer();
}

void Guy::timerStart()
{
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(onTimeOutTimer()));
    this->timer->setInterval(1000);
    this->timer->start();
    std::cout << "starting timer" << std::endl;
}

void Guy::onTimeOutTimer()
{
    std::cout << "check" << std::endl;
}

But as an ouput, I get this error: 但是作为输出,我得到了这个错误:

No matching function for call to 'QObject::connect(QTimer*&, const char*, Guy* const, const char*)'

As I undertsand it is that QTimer is no QObject required as first input of the function connect() , but the documentation specifies QTimer inherits from QObject . 正如我所了解的, QTimer不需要QObject作为函数connect()第一个输入,但是文档指定QTimerQObject继承。 I have no clue here. 我这里没有头绪。

You will need to inherit from QObject, too, to get this working as signals and slots are availabl for QObjects. 您还需要从QObject继承,以使此工作正常进行,因为QObjects可以使用信号和插槽。 QGraphicsItem does not inherit QObject , not even indirectly. QGraphicsItem不继承QObject ,甚至不间接继承。

Not only that, you will also need to add the Q_OBJECT macro as follows: 不仅如此,您还需要添加Q_OBJECT宏,如下所示:

class Guy : public QObject, public QGraphicsItem
{
    Q_OBJECT
    ...
}

or even better because QGraphicsObject inherits QObject and QGraphicsItem. 甚至更好,因为QGraphicsObject继承了QObject和QGraphicsItem。

...
#include <QGraphicsObject>
...

class Guy : public QGraphicsQObject
{
    Q_OBJECT
    ...
}

Also, if you make this change, I suggest to change the QObject::connect to connect as you do not need to indicate the QObject:: scope then. 另外,如果您进行了更改,我建议更改QObject::connect以进行connect因为此时您无需指示QObject::作用域。

On a side note, including stdio.h does not seem to make sense here. 附带一提,在这里似乎似乎没有必要包含stdio.h

Furthermore, allocating the QTimer instance on the heap looks like wasteful to me. 此外,在堆上分配QTimer实例对我来说似乎很浪费。 It is not only leaking the memory, but also adds additional complexity. 这不仅会泄漏内存,还会增加其他复杂性。 Even if you allocate it on the heap, you should pass this as the parent and use the initializer list or C++11 style initialization. 即使将其分配到堆上,也应将此作为父级传递,并使用初始化列表或C ++ 11样式初始化。 Also, if you allocate it on the heap, you could use forward declaration in the header. 另外,如果在堆上分配它,则可以在标头中使用前向声明。

If the slot is not used outside the class, you should also make that private. 如果没有在班级以外使用插槽,则还应将其设为私有。

It is probably also a bad idea to make the timer member public. 公开计时器成员可能也是一个坏主意。 Hopefully you do not do that. 希望您不要那样做。

You can inherit from QGraphicsObject which provides a base class for all graphics items that require signals, slots and inherits QGraphicsItem and QObject . 您可以从QGraphicsObject继承,后者为所有需要信号,插槽的图形项提供基类,并继承QGraphicsItemQObject Also add Q_OBJECT macro in your class declaration. 还要在类声明中添加Q_OBJECT宏。

If you use new Qt5 style connects as in 如果您使用新的Qt5样式的连接,如下所示

QObject::connect(timer, &QTimer::timeout, this, &Guy::onTimeOutTimer)

The onTimeOutTimer function does not need to be marked as slot, and Guy could stay a non-QObject. onTimeOutTimer函数不需要标记为插槽,并且Guy可以保留为非QObject。 Much slimmer and less macros involved. 所涉及的宏更苗条,更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM