How to kill a timer
-
wrote on 26 Aug 2021, 16:07 last edited by Driftwood
I have this code:
QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(scrollKeeper())); timer->start(80);
As you can see, it triggers
scrollKeeper()
function where the timer continues to run. And run and run and run. I need to be able to kill it inscrollKeeper()
, but I can't seem to make that happen.Is there a way to get the timer's ID so i can use
killTimer(ID)
? Or will that even work withtimer->start()
? -
wrote on 26 Aug 2021, 16:11 last edited by JoeCFD
if ( nullptr == timer ) { **** output error msg ***** } else { if ( timer->isActive() ) { timer->stop(); } }
-
if ( nullptr == timer ) { **** output error msg ***** } else { if ( timer->isActive() ) { timer->stop(); } }
-
I tried that last night and crashed. I guess I have this wired up all wrong, even after reading the docs and viewing a few examples online.
I'll keep on it, though. Thanks for your input.
wrote on 26 Aug 2021, 16:54 last edited by@Driftwood run your code in debugger and you can easily find the problem. call stop() should not crash your app.
-
I tried that last night and crashed. I guess I have this wired up all wrong, even after reading the docs and viewing a few examples online.
I'll keep on it, though. Thanks for your input.
wrote on 26 Aug 2021, 16:57 last edited by JonB@Driftwood said in How to kill a timer:
I tried that last night and crashed
It would crash if
timer == nullptr
, or doesn't point to a valid instance for whatever reason. What is the scope of yourQTimer *timer = new QTimer(this);
, because that's thetimer->
you will need, and it doesn't look like a class member variable? -
@Driftwood said in How to kill a timer:
I tried that last night and crashed
It would crash if
timer == nullptr
, or doesn't point to a valid instance for whatever reason. What is the scope of yourQTimer *timer = new QTimer(this);
, because that's thetimer->
you will need, and it doesn't look like a class member variable? -
@Driftwood said in How to kill a timer:
I tried that last night and crashed
It would crash if
timer == nullptr
, or doesn't point to a valid instance for whatever reason. What is the scope of yourQTimer *timer = new QTimer(this);
, because that's thetimer->
you will need, and it doesn't look like a class member variable?wrote on 26 Aug 2021, 17:06 last edited by Driftwoodsnippet of mainwindow.cpp
void MainWindow::scrollKeeper() { ui->label_About->move(380, g_counter); g_counter -= 1; if (g_counter < 100) { timer->stop(); return; } if (g_counter < -(ui->label_About->height())) g_counter = ui->stackedWidget->height()+6; // qDebug()<< g_counter; } void MainWindow::on_action_About_triggered() { ui->stackedWidget->setCurrentIndex(2); ui->label_About->move(380, ui->stackedWidget->height()); g_counter = ui->stackedWidget->height()+6; // QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(scrollKeeper())); timer->start(30); }
snippet of the header file:
private: Ui::MainWindow *ui; int g_counter; QTimer *timer;
In the second function up there, it crashes at
timer->start(30);
with this error:QObject::connect: Cannot connect (null)::timeout() to MainWindow::scrollKeeper()
A for loop will work, if need be. And it's looking more and more that way =)
-
snippet of mainwindow.cpp
void MainWindow::scrollKeeper() { ui->label_About->move(380, g_counter); g_counter -= 1; if (g_counter < 100) { timer->stop(); return; } if (g_counter < -(ui->label_About->height())) g_counter = ui->stackedWidget->height()+6; // qDebug()<< g_counter; } void MainWindow::on_action_About_triggered() { ui->stackedWidget->setCurrentIndex(2); ui->label_About->move(380, ui->stackedWidget->height()); g_counter = ui->stackedWidget->height()+6; // QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(scrollKeeper())); timer->start(30); }
snippet of the header file:
private: Ui::MainWindow *ui; int g_counter; QTimer *timer;
In the second function up there, it crashes at
timer->start(30);
with this error:QObject::connect: Cannot connect (null)::timeout() to MainWindow::scrollKeeper()
A for loop will work, if need be. And it's looking more and more that way =)
wrote on 26 Aug 2021, 17:19 last edited by JoeCFD@Driftwood
private:
Ui::MainWindow *m_ui{};
int m_gCounter{};
QTimer *m_timer{};m_timer = new QTimer(this); ===>wrong: QTimer *timer = new QTimer(this);
-
@Driftwood
private:
Ui::MainWindow *m_ui{};
int m_gCounter{};
QTimer *m_timer{};m_timer = new QTimer(this); ===>wrong: QTimer *timer = new QTimer(this);
wrote on 26 Aug 2021, 17:34 last edited by@JoeCFD
Thank you. I see what I did wrong (more than one thing, really). It works nicely now. Before I decided I wanted the text to stop at a certainy axis
, I had it scrolling nicely. I didn't realize the text skidding to a stop wld be so monumental to me.But one last thing: Is there a way to get a timer's ID, or is that even necessary?
-
@JoeCFD
Thank you. I see what I did wrong (more than one thing, really). It works nicely now. Before I decided I wanted the text to stop at a certainy axis
, I had it scrolling nicely. I didn't realize the text skidding to a stop wld be so monumental to me.But one last thing: Is there a way to get a timer's ID, or is that even necessary?
wrote on 26 Aug 2021, 17:54 last edited by@Driftwood timerId(); But what is for?
-
@Driftwood timerId(); But what is for?
-
I was just curious about how to get a timer's ID. This is my first time to use QTimer. I read the docs and looked at a few examples and i still messed it up.
You've been a great help. Thanks again.
1/12