Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pause functionality in Qt using QTimer on button press



  • 0 down vote favorite

    I am trying to implement pause button for a game I am developing in Qt + OpenGL.

    I want to implement it using QTimer. Basically I am updating screen per 100ms. So in order to pause game, I will stop the timer on button press. and when button is again pressed i will start the timer again

    Here is my pauseOrPlay SLOT:
    @
    void Window::pauseOrPlay()
    {
    GLWidget::modifyTimer = TRUE;
    GLWidget::isPaused = !GLWidget::isPaused;

    GLWidget timerUpdater;
    timerUpdater.timerFunc();
    }
    @

    and Here is my timerFunc()

    @
    GLvoid GLWidget::timerFunc()
    {
    static QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));

    if( GLWidget::isPaused)
    timer->start(100);
    else
    timer->stop();

    }
    @

    But I am not getting the functionality. I get paused screen on this particular code and upon trying few tweaks here and there, sometimes I get screen updating very fastly which pointed me to this but I was unable to find cure to my problem somehow

    This maybe bit naive question about Qt, so may be I am missing something very basic. Any help or Pointers??



  • Hi,

    in your code
    @
    void Window::pauseOrPlay()
    {
    GLWidget::modifyTimer = TRUE;
    GLWidget::isPaused = !GLWidget::isPaused;

    GLWidget timerUpdater;
    timerUpdater.timerFunc();
    }
    @

    object timerUpdate goes out of scope and the is destroyed



  • yes, and due to this my @QTimer *timer@ was getting created everytime,

    So what I did was:
    made @QTimer *timer@ member of my class. on buttonpressed called a member function of my class and in that function did timer stop or start.

    Thanks for the help though


Log in to reply