How to properly implement a count down timer in a QLabel - c++

In order to shrink the problem I created a minimal verifiable example below.
I am trying to implement a 60 minutes count down using a QLabel.
The problem I have is that instead of seeing the time going back, I see the the current time going forward. And no countdown goes back.
Below the minimal verifiable example:
mainwindow.h
#include <QMainWindow>
#include <QTime>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void timerUpdate();
private:
Ui::MainWindow *ui;
QTimer *timer;
QTime time;
};
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->countDown->setText(time.currentTime().toString("hh:mm:ss"));
ui->countDown->setText("60:00");
//ui->countDown->setText(time.toString("hh:mm:ss"));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerUpdate()
{
time = time.addSecs(-1);
ui->countDown->setText(time.currentTime().toString("hh:mm:ss"));
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
EDIT 2
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->countDown->setText(time.toString("hh:mm:ss"));
//ui->countDown->setText("60:00");
ui->countDown->setText(time.toString("hh:mm:ss"));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerUpdate()
{
time = time.addSecs(-1);
ui->countDown->setText(time.toString("hh:mm:ss"));
}
I followed exactly the official documentation but despite that it still does not work. In addition to that I consulted also the QTime class to make sure I was respecting the members function to call.
I tried also another thign which is setting the constructor to setHMS and providing the proper values related to 1 hour, but that also didn't work.
I dug more and came across this post which uses a very similar approach to what I used, with the difference that the example implements a current timer and not a count down. That is the reason why in the timerUpdate() function I am diminishing time time = time.addSecs(-1); instead of adding it. But still does not work.
Thanks for pointing in the right direction to solve this problem.

Related

How to run functions in different QThreads

How can I run loop functions in different QThreads? I need to run in different threads because if I don't do it, the loops break.
How to make the on_pushTurnOn_clicked be executed in other thread, and the loop that pushTurnOn must be able to be canceled by on_pushTurnOff_clicked even on a different thread.
MainWindow .h code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QThread> //I don't know how to use it
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushTurnOn_clicked();
void on_pushTurnOff_clicked();
private:
QTimer *timerLoop;
};
#endif // MAINWINDOW_H
MainWindow cpp code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushTurnOn_clicked()
{
timerLoop = new QTimer(this);
timerLoop->setInterval(3000);
timerLoop->setSingleShot(true);
connect(timerLoop, SIGNAL(timeout()),
SLOT(on_pushTurnOn_clicked()));
qDebug()<<"the loop was started";
timerLoop->start(); //start the loop
}
void MainWindow::on_pushTurnOff_clicked()
{
qDebug()<<"the loop was stoped";
timerLoop->stop(); //stop the loop
}
I will start with a side note. Your memory allocation for timerLoop in on_pushTurnOn_clicked() will cause a memory leak due to absence of deallocation, even though it is a single shot timer. You are creating a new QTimer without destroying the previous allocated one. The better approach is to just allocate memory once in the constructor and just start the timer in on_pushTurnOn_clicked().
Coming back to your question, you will have to split your class into two, one is your mainWindow which has the slots for on_pushTurnOn_clicked() and on_pushTurnOff_clicked(), other possible UI elements and non-intensive tasks.
The second class is a worker which contains the QTimer along with the actual work load. So you would essentially have something like this:
MainWindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//constructor
ui->setupUi(this);
this->worker = new Worker(); //object of WorkerClass type
QThread *thread = new QThread();
worker->moveToThread(thread);
connect(this, SIGNAL(startOrStopTimerSignal(bool), worker, SLOT(startStopTimer(bool));
}
void MainWindow::on_pushTurnOn_clicked()
{
if(Busy==1){
qDebug()<<"Is busy!";
}
if(Busy==0){
FuncaoLoop(RandomParameter1, RandomParameter2);
}
//start the timer through a signal (timers need to be started in their own threads)
emit startOrStopTimerSignal(true);
}
void MainWindow::on_pushTurnOff_clicked()
{
emit startOrStopTimerSignal(false); //stop the timer
}
WorkerClass.h/.cpp:
WorkerClass::WorkerClass(QObject *parent) : QObject(parent)
{
timerLoop= new QTimer(this);
connect(timerLoop, SIGNAL(timeout()), this, SLOT(workLoad())); //timer connect to your actual task
}
void WorkerClass::startStopTimer(bool start)
{
if (start)
timerLoop->start();
else
timerLoop->stop();
}
WorkerClass::workLoad()
{
//whatever task for your PLC
}
Hope this helps you out.

Why can I use QSerialPort* as a temporary variable but not as a class' member in Qt 5.5.1?

I was trying to build a GUI for a project with an Arduino. The project itself is about a car-robot for harbor's containers management, and during the time it's on the development stage (and also on final release) would be cool to have a way to monitor and send commands to it. Seems a good idea to initially implement a communication via serial port and then rewrite/reuse the code for a radio communicator or something like that. The code was going well until some bugs come up.
First were used QSerialPort and QSerialPortInfo, following the example of several programs available at Qt's website. The compiler showed up with some errors about a missing "QT+=serialport" and about wrong forward declarations but after a while I could fix them. Then when the code to open a serial port was written, the program started to crash...
After some hours, this GUI Qt code (and a respective console version) works and exemplifies the problem:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort/qserialport.h>
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
QSerialPort *connected_port;
connected_port = new QSerialPort;
}
MainWindow::~MainWindow() {
delete ui;
}
main.cpp
#include <QApplication>
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
However just after changing mainwindow.h and inserting QSerialPort *connected_port; in MainWindows's private section and removing it from mainwindow.cpp, this simple program starts to crash.
mainwindow.h
(...)
class MainWindow : public QMainWindow {
(...)
private:
QSerialPort *connected_port;
Ui::MainWindow *ui;
};
(...)
mainwindow.cpp
(...)
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
connected_port = new QSerialPort;
}
(...)
Will crash. In qt's terminal example is a similar approach, but I did not get the why it works and this code don't. How make it work?
Solved. For some reason, adding a "QT+=widgets" on the .pro file produced a functional program, aside the already added "QT+=serialport".

Singleton thread-safe programs

I'm trying to write a simple app with singleton design in Qt. Below is the header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
static MainWindow *getInstance();
~MainWindow();
private:
explicit MainWindow(QWidget *parent = 0);
static MainWindow *uniqueInstance;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
And here is implementation file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow* MainWindow::uniqueInstance = new MainWindow();
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow* MainWindow::getInstance()
{
return uniqueInstance;
}
Finally here is the main function:
#include <QApplication>
#include "mainwindow.h"
#include "QThread"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *w = MainWindow::getInstance();
w->show();
return a.exec();
}
Building of my program is OK. But I receive a run-time error "QWidget: Must construct a QApplication before a QWidget". What should I do for solving this problem? I want to use this form of singleton to have a thread-safe program.
Thanks in advance for your helps.
Reza
The Qt-idiomatic way of holding a global object instance thread-safely is through Q_GLOBAL_STATIC. The instance is created on the first use. This way, your singleton instance will be created when needed, after QApplication instance exists.
Instead of MainWindow* MainWindow::uniqueInstance = new MainWindow(), you'd write:
Q_GLOBAL_STATIC(MainWindow, uniqueInstance);
Based on previous answers and also http://doc.qt.io/qt-5/qglobalstatic.html#Q_GLOBAL_STATIC, the answer is like below:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
friend class myClass;
public:
static MainWindow *getInstance();
~MainWindow();
private:
explicit MainWindow(QWidget *parent = 0);
Ui::MainWindow *ui;
};
class myClass : public MainWindow
{
};
#endif // MAINWINDOW_H
.cpp file is like below:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGlobalStatic>
Q_GLOBAL_STATIC(myClass,uniqueInstance)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow* MainWindow::getInstance()
{
return uniqueInstance;
}
finally main file is like below:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *w = MainWindow::getInstance();
w->show();
return a.exec();
}
And that works and is thread-safe.
MainWindow* MainWindow::uniqueInstance = new MainWindow();
this is global instance, it happens before the main function, so this constructor is earlier than the main function, which is not allowed. Qt needs to construct QApplication first, then the widget. so you need to move this after constructor the QApplication in main, or just remove it.
MainWindow* MainWindow::uniqueInstance = 0;
then constructor the object after QApplication.
As I said, you should NOT constructor the uniqueInstance before main, modified some code basing on your POST, it should work.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow* MainWindow::uniqueInstance = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow* MainWindow::getInstance()
{
if (0 == MainWindow::uniqueInstance){
MainWindow::uniqueInstance = new MainWindow();
}
return MainWindow::uniqueInstance;
}
Basing on Kuba's POST, there is introduced a new global MACRO for creating this kind of SINGLETON (Q_GLOBAL_STATIC http://qt-project.org/forums/viewthread/13977), it is more elegant, however, this macro only exists in Qt5, not Qt4, and also has some usage limit you should notice. Basically it is also a macro for wrapping code to create singleton at runtime.

Using Signals and Slots to Comunicate a QDialog with MainWindow in Qt

Ok I think is time to get some help, I have been practicing with signals and slots in Qt and I got stocked. What I want is to be able to change a label in mainwindow when a button in a QDialog is clicked. I have been searching and apparently the only way to do this is basically using signals and slots, here is what I have...
I have a mainwindow.ui with a button called "pushButton_OpenWindow" and a QLabel label_ShowText", I also have a externaldialog.ui that contains a QLineEdit called lineEdit_ExternalInput" and a QPushButton called "pushButton_SendText", and what I want is to change "label_ShowText" to whatever value "lineEdit_ExternalInput" is when pushButton_SendText" is clicked but it doesn't work, when I click the button nothing happens no errors, no warnings nothing.
Here is the code that doesn't work...
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_OpenWindow_clicked();
void textValue(const QString &newText);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "externaldialog.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ExternalDialog *externalDialog = new ExternalDialog;
// connecting signals and slots
QObject::connect(externalDialog, SIGNAL(textChanged(QString)), this, SLOT(textValue(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_OpenWindow_clicked()
{
ExternalDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
}
void MainWindow::textValue(const QString &newText)
{
ui->label_ShowText->setText(newText);
qDebug()<<"Message from textValue Function \n";
}
externaldialog.h
#ifndef EXTERNALDIALOG_H
#define EXTERNALDIALOG_H
#include <QDialog>
namespace Ui {
class ExternalDialog;
}
class ExternalDialog : public QDialog
{
Q_OBJECT
public:
explicit ExternalDialog(QWidget *parent = 0);
~ExternalDialog();
private:
Ui::ExternalDialog *ui;
signals:
void textChanged(const QString&);
public slots:
void on_pushButton_SendText_clicked();
};
#endif // EXTERNALDIALOG_H
externaldialog.cpp
#include "externaldialog.h"
#include "ui_externaldialog.h"
#include <QDebug>
ExternalDialog::ExternalDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExternalDialog)
{
ui->setupUi(this);
}
ExternalDialog::~ExternalDialog()
{
delete ui;
}
void ExternalDialog::on_pushButton_SendText_clicked()
{
emit textChanged(ui->lineEdit_ExternalInput->text());
qDebug()<<"Sent Message";
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Any idea what I'm I doing wrong? Any suggestion will be appreciated. Sorry I posted the whole code but sometimes is better to see the whole picture.
Thanks a lot
change function MainWindow::on_pushButton_OpenWindow_clicked() into this
void MainWindow::on_pushButton_OpenWindow_clicked()
{
externalDialog->setModal(true);
externalDialog->exec();
}
You have just create a new unconnected dialog in the original function.

Changing the currentIndex() of a tab widget when a different item is selected from a drop-down box

I'm new to C++ and I'm just start to port a program that was originally in python/Qt to C++/Qt in order to take advantage of a better terminal widget that I can embed in my program. Right now I'm a bit stuck, I'm trying to setup where if a different item from a drop-down box is selected the currentIndex() of a tab widget is changed accordingly.
Heres my code so far:
//main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
heres the mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTimer *timer;
void startMyTimer()
{
timer = new QTimer();
timer->setInterval(1);
timer->start();
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(changeIndex()));
}
private:
Ui::MainWindow *ui;
void changeIndex();
};
#endif // MAINWINDOW_H
And lastly heres the mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
changeIndex();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeIndex()
{
if (ui->comboBox->currentText() == "add-apt-repository")
{
ui->stackedWidget->setCurrentIndex(0);
ui->checkBox->setCheckState(Qt::Checked);
}
if (ui->comboBox->currentText() == "apt-get")
{
ui->stackedWidget->setCurrentIndex(1);
ui->checkBox->setCheckState(Qt::Checked);
}
if (ui->comboBox->currentText() == "aptitude")
{
ui->stackedWidget->setCurrentIndex(2);
ui->checkBox->setCheckState(Qt::Checked);
}
if (ui->comboBox->currentText() == "bzr")
{
ui->stackedWidget->setCurrentIndex(3);
ui->checkBox->setCheckState(Qt::Unchecked);
}
if (ui->comboBox->currentText() == "cd")
{
ui->stackedWidget->setCurrentIndex(4);
ui->checkBox->setCheckState(Qt::Unchecked);
}
if (ui->comboBox->currentText() == "chmod")
{
ui->stackedWidget->setCurrentIndex(5);
ui->checkBox->setCheckState(Qt::Checked);
}
}
I've looked at a bunch of QTimer examples but I'm at a loss.
I also tried doing if (ui->comboBox->changeEvent()) but I was probably using that wrong as well.
First, you probably have to mark changeIndex() as a slot, like this:
class MainWindow : public QMainWindow
{
Q_OBJECT
// ...
private slots:
void changeIndex();
private:
Ui::MainWindow *ui;
}
This also requires you to invoke the Qt meta object compiler. If you use qmake, that's already done for you. Otherwise, it depends on your build system.
Second, is there any particular reason for using the timer? You can also connect to one of the combo box's currentIndexChanged signals.
Drop the timer, it's of no use here.
Instead, make changeIndex() a slot by putting it into a "private slots:" section:
public slots:
void changeIndex();
Then connect the combobox's currentIndexChanged signal to your slot, in the MainWindow constructor:
connect( ui->combobox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeIndex()) );