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;
}