PDA

View Full Version : How to stop a timer?



8Observer8
28th August 2014, 06:49
Hello!

I created the MainWindow project and added:

MainWindow.h


private:
void timerEvent( QTimerEvent *event );


I wrote in the constructor of the MainWindow:


startTimer( 2000 );


I wrote:


void MainWindow::timerEvent(QTimerEvent *event)
{
qDebug() << "timerEvent";
}

How to stop this timer here:


void MainWindow::on_startTransmissionButton_clicked()
{

}


P.S. I cannot find timerEvent in the Qt's documentation

Lesiok
28th August 2014, 07:03
QObject::killTimer

8Observer8
28th August 2014, 07:16
Thank you very much!



void MainWindow::on_startTransmissionButton_clicked()
{
m_timerID = startTimer( 2000 );
}

void MainWindow::on_stopTransmissionButton_clicked()
{
killTimer( m_timerID );
}


Added after 7 minutes:

It is better:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// ...

ui->stopTransmissionButton->setEnabled( false );
}

// ...

void MainWindow::on_startTransmissionButton_clicked()
{
m_timerID = startTimer( 2000 );
ui->startTransmissionButton->setEnabled( false );
ui->stopTransmissionButton->setEnabled( true );
}

void MainWindow::on_stopTransmissionButton_clicked()
{
killTimer( m_timerID );
ui->startTransmissionButton->setEnabled( true );
ui->stopTransmissionButton->setEnabled( false );
}

anda_skoa
28th August 2014, 09:21
Why muck with low level events and not simply use a QTimer?

Cheers,
_