Qt Login Timeout Using QTimer - c++

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()));

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.

using QNetworkAccessManager GET multiple times

I am writing an application to request a web page at equal intervals in order to get any changes in it (to check whether new data is received). here how i did it.
private:
QNetworkReply *r;
QNetworkAccessManager *m;
QNetworkRequest request;
QTimer *timer;
in the constructor ,
m = new QNetworkAccessManager(this);
timer = new QTimer(this);
connect(r , SIGNAL(readyRead()), this , SLOT(readit()));
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
readit function,
void MainWindow::readit(){
QString st;
st=r->readAll();
m->deleteResource(request);
ui->textBrowser->append(st);
}
update function,
void MainWindow::update()
{
request.setUrl(QUrl("http://localhost/test/default.php"));
r = m->get(request);
}
my problem is m->get(request) gets the request at its first call only, when it is called again it does nothing. I did several experiments but end up with no success results. i changed the second request to another web page using a button click but it did nothing too.
So I need help from an expert how to update the get request and get new reply multiple times.
and also i want to know am i doing a correct thing or is there mo reliable methods to get data on data change from the server than checking for the website at regular intervals.
I see following problems:
readyRead fires an arbitrary number of times per request - including zero times (!), but you treat it as if it fired exactly once. Use the finished signal, which is does what you want: fires once, no more, no less.
The update slot doesn't connect any slots to the request.

QTimer - Repetitive Timer

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();

Qtimer fires before QNetworkAccessManager finished

Im making a connection every X sec. but in case net bandwidth is overloaded timer fires before QNetworkAccessManager sends finished signal and app crashes.
MainWindow::construct:
pTimer = new QTimer(this);
connect(pTimer, SIGNAL(timeout()), this, SLOT(connect()));
pTimer->start(5000);
MainWindow::connect()
pNetworkManager = new QNetworkAccessManager(this);
connect(pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(result(QNetworkReply*)));
pNetworkManager->get(QNetworkRequest(url));
MainWindow::result(QNetworkReply *reply) processes the response
how to check if QNetworkAccessManager isFinished before timer fires again?
Don't create a network manager per request, but only one network manager for your class. Otherwise you leak managers with each request until the mainwindow is destroyed.
QNetworkManager::get returns the pointer to the QNetworkReply representing the request. You can store that reply in a QPointer, connect to its signals, check QNetworkReply::isFinished() etc. to track whether the request is still running or not.
Why not simply launch timer after request finished?
pTimer = new QTimer(this);
pTimer->setSingleshot(true);
connect(pTimer, SIGNAL(timeout()), this, SLOT(connect()));
pTimer->start(5000);
pNetworkManager = new QNetworkAccessManager(this);
connect(pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(result(QNetworkReply*)));
connect(pNetworkManager, SIGNAL(finished(QNetworkReply*)), pTimer, SLOT(start()));