Check Screen Idle state



  • How to determine Screen Idle state in Qt.
    Is there any signal which will be emitted when the device goes to the idle state.





  • I have tried using Qtimer, below is the sample code.

    void home::resetTimer(int idle_time)
    {
    homeTimer = new QTimer(this);
    QTimer::singleShot(idle_time, this, SLOT(idlestate()));
    homeTimer->start(idle_time);
    }

    void home::idlestate()
    {
    anm->animationTimer->start(500);
    MainScreen = true ;
    m_Thread->StartTransaction();
    this->close();emit removeHome();
    }

    I will be calling resetTimer() whenever a key event happens. But the problem is the timer dose not restart to the new time, it ll be running with the previous time value.


  • Moderators

    @Naveen_ks You should delete the old timer before creating new one:

    void home::resetTimer(int idle_time)
    {
        if (homeTimer)
            delete homeTimer;
        homeTimer = new QTimer(this);
        QTimer::singleShot(idle_time, this, SLOT(idlestate()));
        homeTimer->start(idle_time);
    }
    

    But actually there is no need to create new timer each time you call resetTimer.
    Why do you have homeTimer at all? You do not connect any slot to it.



  • @jsulm Actually whenever I see that the screen is idle for few duration it has to go back to main screen, for that purpose i searched for a signal to check for idle condition but could not find. Hence I am using Timer. If there is no event happening then the timer starts running, if not the other event runs.



  • @jsulm The suggested code is not working even if i delete the old timer and create a new timer, the timer is running in the background.
    If you have any other solution please share it. It will be helpfull.


  • Moderators

    @Naveen_ks You do not connect any slots to homeTimer, so what is the purpose of homeTimer?
    You should not use singleShot, because each time there is an event you will start another timeout and you cannot stop it!

    QTimer::singleShot(idle_time, this, SLOT(idlestate()));
    

    You should use homeTimer properly:

    // In constructor:
    connect(&homeTimer, SIGNAL(timeout()), this, SLOT(idlestate()));
    
    void home::resetTimer(int idle_time)
    {
        // homeTimer should not be a pointer
        // Just restart the timer
        homeTimer.start(idle_time);
    }
    


  • @jsulm Thanks Jsulm for the solution. It is solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.