QTimer - Repetitive Timer - c++

I'm trying to have an QTimer object count in intervals, continuously to call a function. I followed an example and I have set the intervals but it doesn't appear to start counting ever again.
This is the piece of code I'm working with
QTimer *timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(MyFunction()));
timer->start();

Is your main loop stil running?
Does the object you reference with "this" is stil existent?
Could you check if the timer is set to single shot?

sorry didn't have the function set to a slot in the header file that was the problem
private slot:
void MyFunction();

Related

Qt C++: how to add a simple countdown timer?

I'm new to Qt C++ and from the few resources I found online I wasn't able to extract only the bit I need to add a countdown timer to a form. I'm not trying to add any buttons or other functionality. Only need to have a timer starting at 1:00 and then decreasing until 0:00 is reached, at which point I need to show some sort of message indicating the user the time is up. I thought maybe adding a label to display the timer would be a simple way to do it (but now sure if I'm right on this).
So far I created a new Qt Application project, added a label to my main form and added some timer code to mainwindow.cpp from what I got at http://doc.qt.io/archives/qt-4.8/timers.html:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Initialize "countdown" label text
ui->countdown->setText("1:00");
//Connect timer to slot so it gets updated
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(updateCountdown()));
//It is started with a value of 1000 milliseconds, indicating that it will time out every second.
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateCountdown()
{
//do something along the lines of ui->countdown->setText(....);
}
In mainwindow.h I added a QTimer *timer; as a public attribute and also void updateCountdown(); as a private slot.
But I'm not exactly sure about how to go on from here. I think the next step is to decrease the timer each second and show that on the "countdown" label (which would be done on the updateCountdown slot) but I can't find out how.
I'm also not sure how to trigger a message (maybe on a QFrame) when the countdown gets to 0:00.
From QTimer documentation, the function updateCountdown() is called every 1 second in your configuration. So you should decrease one second from your timer every time this function is called and update in the UI. Currently you are not storing your time anywhere, so I suggest you add it as a global for now, like QTime time(0, 1, 0) QTime Documentation.
Then inside updateCountdown(), call time.addSecs(-1); and then ui->countdown->setText(time.toString("m:ss"));. Then is easy to check if it is "0:00" and do something else.
I hope this helps

QNetworkAccessManager used by QTimer causes falling

I am using QNetworkAccessManager in function, which is run periodically by QTimer. The code is:
QTimer* timer=new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(findUpdate()));
timer->setSingleShot(false);
timer->start(frequency*1800000);
void MainWindow::findUpdate()
{
for (int i=0;i<aplikace.count();i++){
QNetworkAccessManager* manager=new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(checkUpdate(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://www.gibucsoft.8u.cz/"+lang+"gibuclauncher/verze.php?ver="+aplikace.at(i))));
}
}
The compilation is fine and when I run my application from the Qt Creator it is OK as well but when I run it from the OS the program falls after a while (but not long enough for the timer to even start the function findUpdate). Sometimes before falling this error message shows:
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

How to pass an extra variable to a Qt slot

I would like to know how to pass a separate variable into a slot.
I cant seem to get it to work. Is there some way around this?
This is my code:
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(method(MYVARIABLE)));
timer->start(4000);
If you don't want to declare MYVARIABLE in your class, but instead to have it tied to this particular signal/slot connection, you can connect the signal to a C++11 lambda using Qt5's new singal/slot syntax and then call your slot with that lambda.
For example you could write:
QTimer * timer = new QTimer();
connect(timer, &QTimer::timeout, [=]() {
method(MYVARIABLE);
});
timer->start(4000);
Another solution if you can't use C++11 and Qt5 is to use Qt's Property System to attach a variable to your QTimer*. This can be done with QObject::setProperty().
Then in the slot you could use QObject::sender() to get your QTimer* and read the property back using QObject::property().
However, note that it's not a very clean solution, and borderline abuse of the property system.
from http://doc.qt.io/qt-5/signalsandslots.html
The rule about whether to include arguments or not in the SIGNAL() and
SLOT() macros, if the arguments have default values, is that the
signature passed to the SIGNAL() macro must not have fewer arguments
than the signature passed to the SLOT() macro.
you can try this
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(methodSlot()));
timer->start(4000);
methodSlot()
{
method(MYVARIABLE);
}

Qt Login Timeout Using QTimer

I am trying to implement a login timeout when the username/password has been entered wrong too many time. But i am unable to do so. My code is below
ui->label->setText("Password entered wrong too many times, entered 10 minute cooldown period");
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(6000);
ui->pushButton->setVisible(false);
if(!timer->isActive())
ui->pushButton->setVisible(true);
Try this
ui->pushButton->hide();
QTimer::singleShot(5000, ui->pushButton, SLOT(show()));

Using QTimer for displaying timer

I created a class called aTimer which inherits from QTimer. I want to be able to store the time elapsed into a variable called TimeElapsed of type int. I then want to have the timer automatically started when the main window opens and for it to display the time elapsed therein.
I think I'm using the wrong timer, and I am quite confused as to which tools to be using within Qt because there are different ways of handling time. Suffice it to say that I want a stop-watch kind of module that allows me to start and stop time manually without a cap (or an interval with the case of Timer). How shall I proceed? So far, attempts to use QTimer are fruitless.
You do not really need a derived class for this task. I'd probably use a QTimer and a QElapsedTimer.
Create them in your main window constructor and set the QTimers interval according to how often the time should be updated. Also connect its timeout() signal to a function updating the displayed value. In this function you can get the elapsed time from the QElapsedTimer and update the display.
// *.h
QTimer* timer;
QElapsedTimer *eltimer;
// *.cpp
constructor(){
this->timer = new QTimer(this);
this->timer->setInterval(1000);
connect(this->timer, SIGNAL(timeout()), this, SLOT(update_ui()));
this->timer->start();
this->eltimer = new QElapsedTimer(this);
this->eltimer->start();
}
SLOT update_ui(){
qint64 msecs_elapsed = this->eltimer->elapsed();
// Insert value into ui object
}
Of course you can create some buttons to start() and stop() the QTimer