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.
Related
I have 4 buttons on my main window. Each button opens its own window with its own data. How to identify the pressed button to open right window? For example: I press sales button and it opens a window that shows information about ticket sales.
Mainwindow ui
Here is my code from mainwindow h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <sales.h>
#include <theatres.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void button_pressed();
private:
Ui::MainWindow *ui;
sales *s;
theatres *t;
};
#endif // MAINWINDOW_H
And here is my code from mainwindow cpp:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "build/sqlite/sqlite3.h"
#include <QtSql/QSqlDatabase>
#include <QTableView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect((*ui).pushButton,SIGNAL(released()), this, SLOT(button_pressed()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::button_pressed()
{
s = new sales(this);
s -> show();
}
As Andy Newman already answered
the shortest solution is a lambda function
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QHBoxLayout *h_layout = new QHBoxLayout;
centralWidget()->setLayout(h_layout);
for(int c =1; c <= 10; c++)
{
QPushButton *button = new QPushButton(this); // create button
button->setText(QString::number(c)); // set button id
h_layout->addWidget(button); // add a button to the form
// lambda magic
/* connecting a button signal to a lambda function that captures a pointer to a
button and invokes an arbitrary type function. */
connect(button, &QPushButton::clicked, [this, button]() {
pressedButton(button->text());
});
}
}
void MainWindow::pressedButton(const QString &id_button)
{
qDebug("Pressed button: %ls", id_button.utf16());
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "widget.h"
#include "./ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->btn_0,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_1,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_2,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_3,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
}
Widget::~Widget()
{
delete ui;
}
void Widget::SlotButtonClicked()
{
auto sender = this->sender();
if ( sender == ui->btn_0 ) {
// Click btn_0 to open widget0
} else if ( sender == ui->btn_1 ) {
// Click btn_1 to open widget1
} else if ( sender == ui->btn_2 ) {
// Click btn_2 to open widget2
} else if ( sender == ui->btn_3 ) {
// Click btn_3 to open widget3
}
}
If you can use Qt Designer, the best way to do this is to click with button right on the QPushButton (On .ui file in Qt Designer) and click to "Go to Slot", this will create a private slot to this button! In the header file will create the definition, like this:
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
And in the source file (.cpp) will create the "function" clicked pushButton:
void MainWindow::on_pushButton_clicked()
{
}
void MainWindow::on_pushButton_2_clicked()
{
}
void MainWindow::on_pushButton_3_clicked()
{
}
void MainWindow::on_pushButton_4_clicked()
{
}
Inside of the "function" in .cpp, you put the task that you want this button to do, in this case, to open a new window!
When you click "go to slot" in another button, will create another private slot with the respective number (If is the second QPushButton that you create, the private slot will be called by pushButton_2).
The usual way to do this would be to connect the 4 different buttons to 4 different slots. It looks like you are using QtDesigner so that shouldn't be an issue.
If you were generating an array of buttons at run time you'd run into problems and would need a different solution. You could try something like this to pass an array index to the function, for example:
connect(button[x], &QPushButton::clicked, this, [this, x]() { button_pressed(x); });
Or you could do it the Qt way, which would be to call ::setProperty to store data in the button, and then retrieve it from the event, but it's so esoteric that I can't actually remember how to do that...
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;
}
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.
After hours of debugging and searching for an answer on the web, I am a little bit in despair. This is my first small Qt project and what I try to do is the following:
I want to build a little calculation game. It looks like this:
Application before first click
After clicking on "Check / New" there should be a change to the following: Application after first click
But often I do have to click on that button multiple times, to get it triggered. The "Quit" button works perfectly fine, so it does not seem to be a general problem, but probably shitty coding from my side.
Here is my code, I hope you have got some hints:
(1) 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:
Ui::MainWindow *ui;
int a, b, result;
private slots:
void CmdCheckNewClicked();
};
#endif // MAINWINDOW_H
(2) mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <ctime>
void MainWindow::CmdCheckNewClicked() {
if(result != -1) {
int eingabe = ui->EdtInput->text().toInt();
QString comment;
if(eingabe==result) {
comment = "Right";
} else {
comment = "Wrong";
}
comment += QString(": %1 + %2 = %3").arg(a).arg(b).arg(result);
ui->LblComment->setText(comment);
ui->EdtInput->setText("");
}
a = rand() % 20 + 20;
b = rand() % 20 + 20;
result = a + b;
QString task = QString("%1 + %2").arg(a).arg(b);
ui->LblTask->setText(task);
ui->EdtInput->setFocus();
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
srand((unsigned)time(NULL));
connect(ui->CmdCheckNew, SIGNAL(clicked()),
SLOT(CmdCheckNewClicked()));
connect(ui->CmdQuit, SIGNAL(clicked()), SLOT(close()));
}
MainWindow::~MainWindow() {
delete ui;
}
Also in the further course of use of that application, during submitting of results, that PushButton does not execute properly everytime. I really have no more ideas for further debugging.
Sometimes, when you change objects "created" directly in Ui the functions does not work properly. To check if that the case, place a qDebug("here"); in the beggining of your SLOT function. If the debug is triggered, the connection is not the problem, then the fastest solution would be create your Ui element in the MainWindow object.
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();
}