3
votes

I am trying to connect a signal and slot using a QTimer as the sender of a signal. Unfortunately, when I compile the code below, the program runs, but I receive a warning: "no such slot QObject::flip() in game.cpp".

It seems that my slot is not properly defined. Using a Youtube tutorial about QTimer, it sounded as though I needed to add the "Q_OBJECT" macro inside the game class (this is commented out below). However, if I uncomment it, the program fails to compile, providing the error message: "undefined reference to 'vtable for Game'".

How do I properly connect my signal and slot for the timer?

game.h

#ifndef GAME_H
#define GAME_H

#include "player.h"
#include <QtCore>

class Game : public QObject {

    //Q_OBJECT

public:
    Game();
    void timed_job();

public slots:
    void flip();

private:
    bool is_game_on;
    QTimer *timer;

    Player player_1;
    Player player_2;
    Player player_3;
};

#endif // GAME_H

game.cpp

#include "game.h"
#include <QtCore>

Game::Game() {
    is_game_on = true;
}

void Game::timed_job() {
    timer = new QTimer(this);
    timer->start(1000);
    connect(timer, SIGNAL(timeout()), this, SLOT(flip()));
}

void Game::flip() {
    if(is_game_on == true) {
        is_game_on = false;
    }
    else {
        is_game_on = true;
    }
}
1

1 Answers

5
votes

You need Q_OBJECT macro for the slot to be recognized.

The compile time error is caused by missing MOC generated units. Uncomment the line and delete the Makefile generated to force qmake to regenerate the necessary links.