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. Pause functionality in Qt using QTimer on button press
Qt 6.11 is out! See what's new in the release blog

Pause functionality in Qt using QTimer on button press

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.5k Views 1 Watching
  • 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
    Andrew
    wrote on last edited by
    #1

    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??

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Andrew
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

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