Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QTimer in QWidget not calling the function.
QtWS25 Last Chance

Problem with QTimer in QWidget not calling the function.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 115 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnthonyL
    wrote on last edited by
    #1

    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 timer

    So the GameManager loop is never called.

    What would I be doing wrong ?

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      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)

      A 1 Reply Last reply
      1
      • GrecKoG GrecKo

        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)

        A Offline
        A Offline
        AnthonyL
        wrote on last edited by
        #3

        @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 :)

        1 Reply Last reply
        0
        • A AnthonyL has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved