C++ Timer difficulties - c++

I'm having issues implementing timing. What i want to do is run this:
//relay 1 power cycle
void MainWindow::relay1PCycle()
{
relay1High();
while (true){
QTimer::singleShot(750, this, SLOT(relay1Low()));
QTimer::singleShot(750, this, SLOT(relay1High()));
}
}
as while a button is pressed.
//power cycle button
void MainWindow::on_pushButton_3_toggled(bool checked)
{
if (checked){
//run relay1PCycle as a thread.
}
else{
//Terminate relay1PCycle thread.
}
}
however, so far everything i've tried so far has been a failure.
What's the correct way to go about this?
Thanks

The task they want to implement is not blocking, so it is not necessary to use threads, only a timer with an adequate logic. To make it simple we will assign that task to the next class:
relaycontroller.h
#ifndef RELAYCONTROLLER_H
#define RELAYCONTROLLER_H
#include <QObject>
#include <QTimer>
#include <QDebug>
class RelayController : public QObject
{
Q_OBJECT
public:
explicit RelayController(QObject *parent = 0):QObject(parent){
timer = new QTimer(this);
state = false;
timer->setInterval(750);
//old style
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
//new style
//connect(timer, &QTimer::timeout, this, &RelayController::onTimeout);
}
void start(){
qDebug()<<"timer";
timer->start();
}
void stop(){
timer->stop();
}
void relayLow(){
//code here: relay off
qDebug()<<"low";
}
void relayHigh(){
//code here: relay on
qDebug()<<"high";
}
private slots:
void onTimeout(){
if(state)
relayHigh();
else
relayLow();
state = !state;
}
private:
QTimer *timer;
bool state;
};
#endif // RELAYCONTROLLER_H
Then an object is created and used in the slot pressed:
mainwindow.h
private:
RelayController *controller;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
controller = new RelayController(this);
ui->pushButton_3->setCheckable(true);
}
void MainWindow::on_pushButton_toggled(bool checked)
{
if(checked)
controller->start();
else
controller->stop();
}

Related

Qt/C++ How can I disconnect a QProgressDialog::canceled signal to its QProgressDialog::cancel slot?

I have a QProgressDialog and I would like to override its cancel() slot to change its behavior.
Instead of closing the dialog, I would like to do some other operations and then close the dialog after a QThread to finish before closing the dialog.
I tried to disconnect the canceled/cancel signal/slot couples and the reconnect with the new behavior but it does not seem to change much.
As as soon as I click on the cancel button, the progress dialog gets closed first and then my lambda get executed anyway.
Qobject::disconnect(m_progressdialog, &QProgressDialog::canceled, m_progressdialog, &QProgressDialog::cancel);
Qobject::connect(m_progressdialog, &QProgressDialog::canceled, [](){
// continue displaying the dialog as an occupation bar
m_progressdialog->setValue(0);
// do some other things
// a lot of code
// ...
// only later close the dialog
m_progressdialog->close();
});
Is there a way to do this correctly?
I don't know your whole code, but according to the documentation, the idea is more or less the same you're saying: a slot to connect the signal QProgressDialog::canceled().
The following code is just an example but it's working. In this case, instead of using the own Qt property wasCanceled, it is used a boolean to control when to stop and cancel the QProgressDialog.
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class QProgressDialog;
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private slots:
void on_pushButton_clicked();
void my_custom_cancel();
private:
Ui::Dialog *ui;
QProgressDialog *progress;
int numTasks = 100000;
bool canceled = false;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QProgressDialog>
#include <QThread>
#include <QDebug>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
progress = new QProgressDialog("Task in progress...", "Cancel", 0, numTasks);
connect(progress, SIGNAL(canceled()), this, SLOT(my_custom_cancel()));
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
progress->open();
for (int i = 0; i < numTasks; i++) {
progress->setValue(i);
QThread::usleep(100);
if (canceled)
break;
}
progress->setValue(numTasks);
}
void Dialog::my_custom_cancel()
{
qDebug() << "do something";
canceled = true;
}

Qt signal and slots do not work if called from QRunnable or another thread

I am trying to learn how to use Multi-threading in Qt and put it to use in QtWidget applications. So I have setup this simple test case.
MainWindow
This form has some buttons and each button will execute another form (a Dialog for that matter).
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->btnShowCalibrationDialog, &QPushButton::clicked,
this, [&](){
CalibrationDialog dlg;
dlg.exec();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
CalibrationDialog This dialog has a QPushButton and a QTextEdit. When the button is clicked I will run a QRunnable class (comes next!)
calibrationdialog.h (I have spared you the includes and guards!)
class CalibrationDialog : public QDialog
{
Q_OBJECT
public:
explicit CalibrationDialog(QWidget *parent = nullptr);
~CalibrationDialog();
public slots:
void onBeginWorkRequested();
void onReceiveData(int data);
private:
Ui::CalibrationDialog *ui;
};
calibrationdialog.cpp
#include "worker.h"
CalibrationDialog::CalibrationDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::CalibrationDialog)
{
ui->setupUi(this);
connect(ui->btnBegin, &QPushButton::clicked,
this, &CalibrationDialog::onBeginWorkRequested);
}
CalibrationDialog::~CalibrationDialog()
{
delete ui;
}
void CalibrationDialog::onBeginWorkRequested()
{
qDebug() << "CalibrationDialog::onBeginWorkRequested()" << "on" << QThread::currentThreadId();
Worker* worker = new Worker();
QThreadPool* pool = QThreadPool::globalInstance();
connect(worker, &Worker::reportProgress,
this, &CalibrationDialog::onReceiveData);
pool->start(worker);
pool->waitForDone();
}
void CalibrationDialog::onReceiveData(int data)
{
ui->teResults->append(QString::number(data));
ui->teResults->ensureCursorVisible();
}
Worker And this is some runnable...I want to report the progress so that is shows up in the textedit of the dialog in a reponsive manner!
worker.h
class Worker : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
void run() override;
private:
int mProgress = 0;
signals:
void reportProgress(int progress);
};
worker.cpp
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::run()
{
qDebug() << "Worker is running #" << QThread::currentThreadId();
while(true) {
mProgress += 100;
emit reportProgress(mProgress);
QThread::msleep(500);
}
}
I see in the debugger that control goes in to the while loop...but the signal is not being handled by the dialog! I mean the textedit stays empty!
I tried to read documentationand search online...I came to the conclusion that my problem lies in the waste land of thread affinity....but I have no idea if that is the case and if that is, how to solve it. Please assist!
remove pool->waitForDone();, never use waitForX methods in a GUI since they are blocking preventing signals from doing their job.

Trigger different event on QTimer reset

I am building a little program that shows traffic light images using QTimer. So i setup my timer and everything works good. But I cant figure out, how can I get the Robot Lights to ->show() and ->hide() each time the timer interval is reached. I can have this all wrong, im still learning so please advise.
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();
void timer();
QTimer *mytimer;
private:
Ui::MainWindow *ui;
int timerValue;
private slots:
void showGreen();
void showYellow();
void showRed();
void on_startButton_clicked();
};
#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);
ui->green->setVisible(false);
ui->yellow->setVisible(false);
ui->red->setVisible(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timer(){
ui->green->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showYellow()));
mytimer->start(timerValue = (ui->spinBox->value())*1000);
}
void MainWindow::showYellow(){
ui->yellow->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showRed()));
mytimer->start(timerValue);
}
void MainWindow::showRed(){
ui->red->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showGreen));
mytimer->start(timerValue);
}
void MainWindow::showGreen(){
ui->green->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showYellow()));
mytimer->start(timerValue);
}
void MainWindow::on_startButton_clicked()
{
timer();
ui->startButton->setEnabled(false);
}
I Also disable the Start button when clicked so the timer cannot run twice.
So On the main Window UI I basicly Have a Spinbox that takes the input of the user which is the time that I pass that to Qtimer, and then I have the 3 images with lights red green yellow that has to show and hide in the intervals . So I created almost something like a manual loop. Qtimer starts and shows Green, then goes to ShowYellow Slot and then showRed slot, so now, its suppose to go to the Green slot and then to yellow, but it doesnt go to Green again.
Can someone tell me why not.
Read the comments:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
...
mytimer = new QTimer(this); // create QTimer once
}
void MainWindow::timer() {
timerValue = ui->spinBox->value() * 1000;
showGreen(); // reuse showGreen()
}
void MainWindow::showYellow() {
// this is how toggling can be done
ui->yellow->setVisible(!ui->yellow->isVisible());
mytimer->disconnect(); // disconnect before making a new connection
connect(mytimer ,SIGNAL(timeout()), this, SLOT(showRed()));
mytimer->start(timerValue);
}
void MainWindow::showRed() {
ui->red->setVisible(!ui->red->isVisible());
mytimer->disconnect();
connect(mytimer ,SIGNAL(timeout()), this, SLOT(showGreen()));
mytimer->start(timerValue);
}
void MainWindow::showGreen() {
ui->green->setVisible(!ui->green->isVisible());
mytimer->disconnect();
connect(mytimer ,SIGNAL(timeout()), this, SLOT(showYellow()));
mytimer->start(timerValue);
}
Perhaps most simply:
void MainWindow::showHide(){
ui->green->setVisible(!ui->green->isVisible());
}
The show() and hide() member functions of QWidget are equivalent to setVisible(true) and setVisible(false), respectively.
A problem you may run into later, if you press the start button more than once, is that you spawn a new interval timer every time, leading to quicker blinking with every press of the button. If this is not what you want to happen, you'll have to control the generation of new timers.

Program runs but nothing displays Qt c++

Hi my program runs with no errors but nothing is displayed on screen, a window is meant to pop up which runs another program with a QProcess, this program runs fine. code follows:
header file:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtGui>
#include <QTimer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void setTimer();
void displayAdvice();
void cancelTimer();
void addAdvice();
void processDone(int);
private:
QLabel* timerLbl;
QLineEdit* timerEdt;
QTextEdit* adviceEdt;
QPushButton* okBtn;
QPushButton* cancelBtn;
QTimer* timer;
QProcess *process;
void setupGui();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setupGui();
}
Widget::~Widget()
{
delete ui;
}
void Widget::setupGui()
{
timerLbl = new QLabel("Timer set interval in seconds");
timerEdt = new QLineEdit();
adviceEdt = new QTextEdit();
this->adviceEdt->setReadOnly(true);
okBtn = new QPushButton("OK");
cancelBtn = new QPushButton("Cancel");
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(timerLbl);
layout->addWidget(timerEdt);
layout->addWidget(okBtn);
layout->addWidget(adviceEdt);
layout->addWidget(cancelBtn);
this->setWindowTitle("Advice");
this->setLayout(layout);
connect(okBtn,SIGNAL(clicked()), this, SLOT(setTimer()));
connect(cancelBtn, SIGNAL(clicked()), this, SLOT(cancelTimer()));
connect(timer, SIGNAL(timeout()),this, SLOT(displayAdvice()));
}
void Widget::setTimer()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()),this, SLOT(cancelTimer()));
QString setting = this->timerEdt->text();
bool ok;
int set = setting.toInt(&ok,10);
set = set * 1000;
timer->setInterval(set);
timer->start();
timerEdt->setReadOnly(true);
okBtn->setDown(true);
}
void Widget::cancelTimer()
{
timer->stop();
adviceEdt->clear();
okBtn->setDown(false);
timerEdt->clear();
timerEdt->setReadOnly(false);
}
void Widget::displayAdvice()
{
process = new QProcess(this);
process->start("C:/Users/Dmon/Desktop/47039949 COS3711 Assignment 2/Question 4/Ass2Q4Part1-build-desktop/debug/Ass2Q4Part1.exe");
connect(process, SIGNAL(readyReadStandardOutput()),this, SLOT(addAdvice()));
connect(process, SIGNAL(finished(int)),this, SLOT(processDone(int)));
}
void Widget::addAdvice()
{
QByteArray data = process->readAllStandardOutput();
QString strToWrite = data;
this->adviceEdt->clear();
this->adviceEdt->append(strToWrite);
}
void Widget::processDone(int)
{
process->close();
process->deleteLater();
process=0;
}
main.cpp
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Program runs with nothing displayed then eventually exits with no errors after about 15 seconds.
Your problem is that you're already using a user interface coming from the form file (.ui file). You need to decide which one you wish to use.
To fix your code, all you need to do is remove all references to the Ui namespace. Simply remove the below:
namespace Ui {
class Widget;
}
//
private:
Ui::Widget *ui;
//
#include "ui_widget.h"
//
delete ui;
Also, note that this line is working with a null or undefined pointer value - you never create the timer instance:
connect(timer, SIGNAL(timeout()),this, SLOT(displayAdvice()));
Finally, there's no reason whatsoever to allocate the member widgets on the heap. It does, in fact, waste a bit of heap memory since QWidget instances are very small.
Here's how your code could look. I've put it all in a single file, to keep it short. You obviously don't need it in a single file. I've also made the UI a bit more compliant with usual expectations. E.g. controls that can't be interacted with should be disabled.
// main.cpp
#include <QApplication>
#include <QtWidgets>
#include <QTimer>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
private slots:
void setTimer();
void displayAdvice();
void cancelTimer();
void addAdvice();
void processDone(int);
private:
QLabel m_timerLbl;
QLineEdit m_timerEdt;
QTextEdit m_adviceEdt;
QPushButton m_okBtn;
QPushButton m_cancelBtn;
QTimer m_timer;
QProcess m_process;
void setupGui();
};
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
setupGui();
}
void Widget::setupGui()
{
m_timerLbl.setText("Timer set interval in seconds");
m_adviceEdt.setReadOnly(true);
m_okBtn.setText("OK");
m_cancelBtn.setText("Cancel");
m_cancelBtn.setEnabled(false);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(&m_timerLbl);
layout->addWidget(&m_timerEdt);
layout->addWidget(&m_okBtn);
layout->addWidget(&m_adviceEdt);
layout->addWidget(&m_cancelBtn);
setWindowTitle("Advice");
connect(&m_okBtn, SIGNAL(clicked()), SLOT(setTimer()));
connect(&m_cancelBtn, SIGNAL(clicked()), SLOT(cancelTimer()));
connect(&m_timer, SIGNAL(timeout()), SLOT(displayAdvice()));
connect(&m_timer, SIGNAL(timeout()), SLOT(cancelTimer()));
connect(&m_process, SIGNAL(readyReadStandardOutput()), SLOT(addAdvice()));
connect(&m_process, SIGNAL(finished(int)), SLOT(processDone(int)));
}
void Widget::setTimer()
{
QString const setting = m_timerEdt.text();
bool ok;
int set = setting.toInt(&ok,10) * 1000;
m_timer.setInterval(set);
m_timer.start();
m_timerEdt.setEnabled(false);
m_okBtn.setEnabled(false);
m_cancelBtn.setEnabled(true);
}
void Widget::cancelTimer()
{
m_timer.stop();
m_adviceEdt.clear();
m_timerEdt.clear();
m_okBtn.setEnabled(true);
m_timerEdt.setEnabled(true);
m_cancelBtn.setEnabled(false);
}
void Widget::displayAdvice()
{
m_process.start("bash", QStringList() << "-c" << "echo 'Hello!'");
#if 0
m_process.start(QDir::homePath() +
"/Desktop/47039949 COS3711 Assignment 2/Question 4/Ass2Q4Part1-build-desktop/debug/Ass2Q4Part1.exe");
#endif
}
void Widget::addAdvice()
{
QByteArray const data = m_process.readAllStandardOutput();
m_adviceEdt.setPlainText(QString::fromLocal8Bit(data));
}
void Widget::processDone(int)
{
m_process.close();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"

Qt QLCDNumber time_t Timer

I have a tool button in one .cpp (i.e. ExerciseControl.cpp). When the button is clicked, it will trigger two timers in another .cpp (i.e. StatusBar.cpp).
However, the following codes does not work. The timers do not start when I click the button in ExerciseControl.cpp to trigger startExercise().
ExerciseControl.cpp:
ExerciseControl::ExerciseControl(void)
{
myStatusBar = new StatusBar;
}
void ExerciseControl::startExercise()
{
myStatusBar ->simulationTimer->start(1000);
myStatusBar ->elapsedTimer->start(1000);
}
StatusBar.cpp:
StatusBar::StatusBar()
{
simulationTimer = new QTimer;
QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
elapsedTimer = new QTimer;
QObject::connect(elapsedTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
createButtons();
};
void StatusBar::createButtons()
{
...
}
void StatusBar::tickSimulation()
{
...
}
However, if I declare the following in my constructor, the timers can start automatically, which is not I wanted.
StatusBar::StatusBar()
{
simulationTimer = new QTimer;
simulationTimer->start(1000);
QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
elapsedTimer = new QTimer;
elapsedTimer->start(1000);
QObject::connect(elapsedTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
}
I need to trigger the timers through my button in ExerciseControl.cpp.
Please help.
Thanks.
On the example below, ExerciseControl is a specialization of QMainWindow that does 2 things:
Display a QTooButton inside the window;
Allocate a StatusBar object.
Clicking the button invokes the slot _timers_must_start() of the class. This slot is responsible to call start_timers() from the StatusBar object, which in turn starts the timers of that object.
I made each timer call a slot that prints something to the screen when the timeout() is triggered.
Enjoy.
main.cpp:
#include "exercise_control.h"
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
ExerciseControl ec;
ec.show();
return app.exec();
}
exercise_control.h:
#pragma once
#include "status_bar.h"
#include <QMainWindow>
#include <QToolButton>
class ExerciseControl : public QMainWindow
{
Q_OBJECT
public:
ExerciseControl()
{
_button = new QToolButton(this);
_button->setText("Press Me!");
QObject::connect(_button, SIGNAL(clicked()), this, SLOT(_timers_must_start()));
_statusbar = new StatusBar;
// Set the size of the window to the size of the button
setFixedSize(_button->width() ,_button->height());
}
private slots:
/* This slot is called by clicking the button */
void _timers_must_start()
{
qDebug() << "ExerciseControl::_timers_must_start: button clicked! Trying to start timers...";
_statusbar->start_timers();
}
private:
QToolButton* _button;
StatusBar* _statusbar;
};
status_bar.h:
#pragma once
#include <QObject>
#include <QTimer>
#include <QDebug>
class StatusBar : public QObject
{
Q_OBJECT
public:
StatusBar()
{
_timer1 = new QTimer;
QObject::connect(_timer1, SIGNAL(timeout()), this, SLOT(_timer1_task()));
_timer2 = new QTimer;
QObject::connect(_timer2, SIGNAL(timeout()), this, SLOT(_timer2_task()));
}
~StatusBar()
{
_timer1->stop();
delete _timer1;
_timer2->stop();
delete _timer2;
}
void start_timers()
{
if (!_timer1->isActive() && !_timer2->isActive())
{
qDebug() << "StatusBar::start_timers: OK!";
_timer1->start(1000); // 1 second interval
_timer2->start(5000); // 5 seconds interval
return;
}
qDebug() << "StatusBar::start_timers: timers already started, nothing to be done here!";
}
private:
QTimer* _timer1;
QTimer* _timer2;
private slots:
/* These slots are called by the timers of this class, hence PRIVATE SLOTS */
void _timer1_task()
{
qDebug() << "StatusBar::_timer1_task: 1 second has elapsed";
}
void _timer2_task()
{
qDebug() << "StatusBar::_timer2_task: 5 seconds has elapsed";
}
};
test.pro:
QT += widgets
SOURCES += \
main.cpp
HEADERS += \
exercise_control.h \
status_bar.h