Max
Max

Reputation: 45

error: no matching function for call to 'QTimer::singleShot'

I have a problem with a class I am trying to implement. Basically, I want to start a singleshot timer when the class gets constructed and connect it to one of the classes slots. At the moment my constructor looks like this:

myclass::myclass(int time)
{
    QTimer::singleShot(time, this, SLOT(myslot()));
}

And produces the error in the title. I already found out that myclass has to be a Q_OBJECT but this didn't fix the error. Any ideas? Thanks in advance!

Upvotes: 3

Views: 3360

Answers (4)

TheDarkKnight
TheDarkKnight

Reputation: 27611

As many of the answers already given have pointed out, QGraphicsItem does not inherit from QObject, which is used for signals and slots.

However, rather than using multiple inheritance with QGraphicsItem and QObject, you could simply derive from the Qt class QGraphicsObject, which is designed to provide a GraphicsItem that requires signals and slots and is itself derived from QGraphicsItem. It also then gives you the QTimer functionality you mention in your question.

Upvotes: 1

CapelliC
CapelliC

Reputation: 60004

QGraphicsItem doesnt't inherits QObject, then you should change myclass

// thanks to WoJo for pointing out the right inheritance order
class myclass : public QObject, public QGraphicsItem
//class myclass : public QGraphicsItem, public QObject
{ Q_OBJECT
...
public slots:
  void myslot();

}

In my experience you'll need to delete the build directory to force a clean rebuild after that.

Upvotes: 2

Zaiborg
Zaiborg

Reputation: 2522

change

QTimer::singleShot(time, this, SLOT(myslot))); // this should give you a syntax error as well

to

QTimer::singleShot(time, this, SLOT(myslot()));

but be aware; it can cause much trouble when invoking its own method after some time. it may come to a race codition when you call some function of that class object and the timer ends and directly calls myslot().

Upvotes: 1

Shar
Shar

Reputation: 455

Did your classe inherit of QObject, QWidget or other widget ? Did you include <QTimer> in your program ?

Upvotes: 1

Related Questions