Problem with QTimer in QWidget not calling the function.
-
Hi,
I am having some problems with a QTimer starting but not calling the function.
I have a window.cpp QWidget parented to the QApplication and a gamemanager.cpp QWidget parented to the window.cpp. The timer in the window.cpp file works but not the one in the gamemanager.cpp
Here is my main function:
#include "game_manager.h" #include "window.h" #include <QApplication> int main(int argc, char** argv) { QApplication app(argc, argv); Window window; window.show(); return app.exec(); }
The <window.h>
#include <QWidget> #include <QTimer> #include <QtDebug> #include "game_manager.h" class Window : public QWidget { Q_OBJECT public: explicit Window(QWidget* parent = nullptr); private slots: void test(); private: GameManager* m_gm; };
the <window.cpp>
#include "window.h" Window::Window(QWidget* parent) : QWidget(parent) { m_gm = new GameManager(this); QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Window::test); timer->start(1000); qDebug() << "Timer Window started now "; } void Window::test() { qDebug()<<"Window timer";}
the <gamemanager.h>
#include <QObject> #include <QTimer> #include <QtDebug> class GameManager : public QWidget { Q_OBJECT public: explicit GameManager(QWidget* parent = 0); QTimer* timer; private slots : void Loop(); };
And finally the <gamemanager.cpp>
#include "game_manager.h" GameManager::GameManager(QWidget* parent) : QWidget(parent) { timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &GameManager::Loop); timer->start(1000); qDebug() << "Timer GM started now "; } void GameManager::Loop() {qDebug()<<"Game manager loop";}
The output gives me:
Timer GM started now
Timer Window started now
Window timer
Window timer
Window timerSo the GameManager loop is never called.
What would I be doing wrong ?
-
Can you double check the content of gamemanager.cpp? My guess is that you didn't paste the actual code and used a temporary QTimer. (or you did that with the GameManager in window.cpp)
-
Can you double check the content of gamemanager.cpp? My guess is that you didn't paste the actual code and used a temporary QTimer. (or you did that with the GameManager in window.cpp)
@GrecKo You are right, the problem lies elsewhere, In an effort to post the minimal code I removed the problem from my code.
It does not seem related to timer anymore but to other functions I call. Now I can find where the problem really comes from.
Thanks :)
-