0
votes

I am trying to implement timeout in QT. I want to perform following task so I require timeout. In application i have implemented menu. If I entered choose of option from menu, it will execute related screen. This screen should timeout after 15 seconds, if I am not getting any key event before 15 seconds. Following is my code:

bool cMeasurementUnit::eventFilter(QObject *obj, QEvent *event)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if(event->type() == QEvent::KeyPress)
    {
        if((keyEvent ->key()) == Qt::Key_Tab)
        {
            if(m_pWidgetFirstTabFocus->hasFocus())
            {
                m_pWidgetFirstTabFocus->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 255, 255);"));
            }
            m_pWidgetFirstTabFocus = m_pWidgetFirstTabFocus->nextInFocusChain() ;
            while((m_pWidgetFirstTabFocus->focusPolicy()) == Qt::NoFocus)
            {
                m_pWidgetFirstTabFocus = m_pWidgetFirstTabFocus->nextInFocusChain() ;
                if(m_pWidgetFirstTabFocus == this)
                {
                    m_pWidgetFirstTabFocus = MEASUREMENT_UNIT_FIRST_TAB;
                }
            }
            m_pWidgetFirstTabFocus->setStyleSheet(QString::fromUtf8("background-color: rgb(207, 207, 207);"));
        }
        else if((keyEvent ->key()) == Qt::Key_Return)
        {
            SaveChannelUnit();
            return true ;
        }
        else if((keyEvent ->key()) == Qt::Key_Up)
        {
            if (((QComboBox *)m_pWidgetFirstTabFocus)->currentIndex() == 0)
            {
                ((QComboBox *)m_pWidgetFirstTabFocus)->setCurrentIndex((((QComboBox *)m_pWidgetFirstTabFocus)->count() - 1)) ;
                return true ;
            }
        }
        else if((keyEvent ->key()) == Qt::Key_Left)
        {
            return true;
        }
    }
    return QObject::eventFilter(obj, event);
}

I have tried to implement using QTimer::singleShot(15000, this, SLOT(DeleteClass())); but it is not working. Please help me regarding this issue.I have implemented QTimer::singleShot inside if(event->type() == QEvent::KeyPress) statement in above code so that whenever I press a key it will reinitialize QTimer::singleShot and screen of class cMeasurementUnit will not get timeout, otherwise it will be timeout after 15seconds. Following is the code for DeleteClass, is it correct? if not will you please tell me correct way to do it? Thanks in advance

void cMeasurementUnit::DeleteClass()
{
    DPRINTF("IN FUNCTION %s\n",__FUNCTION__);
    delete this;
}
1
Deleting an object by itself? That's pointless(unless you have call backs to warn other user objects), use the timeout where you use instances of your class that will fix the issue. And update the screen after the timeout. - Madura Anushanga
The posted source code does not seem to show the problem, you are having. You should create a "Short, Self Contained, Correct (Compilable), Example" sscce.org - bjoernz
handling key events in an event filter? Bad!! There are tons of handlers available. Btw post the code about the timer (connection, DeleteClass) - UmNyobe
hello frank, I have implemented QTimer::singleShot inside if(event->type() == QEvent::KeyPress) statement in above statement so that whenever I press a key it will reinitialize QTimer::singleShot and screen of class cMeasurementUnit will not get timeout, otherwise it will be timeout after 15seconds. Following is the code for DeleteClass, is it correct? if not will you please tell me correct way to do it? Thanks in advance void cMeasurementUnit::DeleteClass() { DPRINTF("IN FUNCTION %s\n",FUNCTION); delete this; } - user1400571
I have edited above post so help me out regarding this problem - user1400571

1 Answers

2
votes

You can use QTimer to run checks periodically and QElapsedTimer to calculate inactivity time.

In the header:

QElapsedTimer elapsed_timer;

In the initialization:

elapsed_timer.start();
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
timer->start(1000); // number of milliseconds between checks
// install event filter for target widget, etc.

In the timeout slot:

if (elapsed_timer.elapsed() > 15000) { // timeout interval in msec
  //perform close actions, e.g. widget->close()
}

In the event filter: the following code should be executed if an appropriate key event was received:

elapsed_timer.restart();