Multiple timers problem
-
I want to use two timers to update graphics on the screen. I have two timers. The first is connected to a slot to change the graphic every 150ms and repeat after 10 iterations. The second is to simulate the completion of internal tests every 2 seconds.
There are a couple of problems which I cannot solve. They are in the Dialog module.
- My timer->stop(); command crashes the program with a "SIGSEGV" segmentation fault.
- My timer2 seems to count up in steps of two rather than one. It skips every second one of the graphics that I give it to display. With the debugger I can stop the code in each state of test but it does not display the graphic.
Could I get some advice please?
Regards,
JamesHere is the Dialog module
@#include <QtGui>
#include <QDialog>
#include <QLabel>
#include <QDebug>
#include <QTimer>#include "dialog.h"
#include "ui_dialog.h"
#include "probe.h"Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
Dialog::setWindowFlags(Qt::SplashScreen); // no decoration on the screen
ui->setupUi(this);// Set up the rolling logo timer QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(state_update())); timer->start(150); // Set up the test results timer QTimer *timer2 = new QTimer(this); connect(timer2, SIGNAL(timeout()), this, SLOT(results_update())); timer2->start(2000);
}
void Dialog::state_update()
{
static int counter=0;switch (counter) { case 1: ui->label->setPixmap(QPixmap(":/logo/logo1.jpg",0,Qt::AutoColor)); break; case 2: ui->label->setPixmap(QPixmap(":/logo/logo2.jpg",0,Qt::AutoColor)); break; case 3: ui->label->setPixmap(QPixmap(":/logo/logo3.jpg",0,Qt::AutoColor)); break; case 4: ui->label->setPixmap(QPixmap(":/logo/logo4.jpg",0,Qt::AutoColor)); break; case 5: ui->label->setPixmap(QPixmap(":/logo/logo5.jpg",0,Qt::AutoColor)); break; case 6: ui->label->setPixmap(QPixmap(":/logo/logo6.jpg",0,Qt::AutoColor)); break; case 7: ui->label->setPixmap(QPixmap(":/logo/logo7.jpg",0,Qt::AutoColor)); break; case 8: ui->label->setPixmap(QPixmap(":/logo/logo8.jpg",0,Qt::AutoColor)); break; case 9: ui->label->setPixmap(QPixmap(":/logo/logo9.jpg",0,Qt::AutoColor)); break; case 10: ui->label->setPixmap(QPixmap(":/logo/logo10.jpg",0,Qt::AutoColor)); break; } if (++counter > 10) counter = 1;
}
void Dialog::results_update()
{
static int test = 0;
QString str;switch (test) { case 0: ui->label_Clk_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor)); break; case 1: ui->label_USB_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor)); break; case 2: ui->label_Probe_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor)); break; case 3: ui->label_HV_Result->setPixmap(QPixmap(":/logo/Fail.jpg",0,Qt::AutoColor)); break; case 4: ui->label_LV_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor)); break; case 5: break; case 6: timer->stop();
// timer2->stop();
Probe *j = new Probe(); j->showMaximized(); this->close(); break; } test++; str = QString("Test = %1").arg(test); ui->label_test->setText(str);
}
Dialog::~Dialog()
{
delete ui;
}void Dialog::on_buttonBox_accepted()
{
qApp->quit();
}@
Here is the Main module
@#include "dialog.h"
#include "probe.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
Dialog Probe;w.show(); return a.exec();
}
@Here is the Probe module
@#include "probe.h"
#include "ui_probe.h"Probe::Probe(QWidget *parent) :
QDialog(parent),
ui(new Ui::Probe)
{
Probe::setWindowFlags(Qt::SplashScreen); // no decoration on the screen
ui->setupUi(this);
}Probe::~Probe()
{
delete ui;
}void Probe::on_buttonBox_accepted()
{
qApp->quit();
}
@Here is Dialog.h
@#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
#include <QtGui>namespace Ui {
class Dialog;
}class Dialog : public QDialog
{
Q_OBJECTpublic:
explicit Dialog(QWidget *parent = 0);
~Dialog();private slots:
void on_buttonBox_accepted();
void state_update();
void results_update();private:
Ui::Dialog *ui;
QDialogButtonBox *buttonBox;
QTimer *timer;
QTimer *timer2;signals:
};
#endif // DIALOG_H
@Here is Probe.h
@#ifndef PROBE_H
#define PROBE_H#include <QDialog>
namespace Ui {
class Probe;
}class Probe : public QDialog
{
Q_OBJECTpublic:
explicit Probe(QWidget *parent = 0);
~Probe();private slots:
void on_buttonBox_accepted();private:
Ui::Probe *ui;
};#endif // PROBE_H
@ -
Hi,
You shadowed both your Dialog timer variable in the constructor so it's no initialized.
Try this:
@
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));
timer->start(150);timer2 = new QTimer(this); connect(timer2, SIGNAL(timeout()), this, SLOT(results_update())); timer2->start(2000);
@
I would recommend to change the class variables naming with something like _timer or m_timer. It helps separating local variables from class variable.
Hope it helps
-
Hi,
Sorry but being new to Qt, I don't understand what you mean by
[quote]You shadowed both your Dialog timer variable in the constructor so it’s no initialized.
[/quote]Can you explain or point me to something where I can learn about this?
Regards,
James -
Hi,
Thanks for your suggestion, that helped with the crashing problem.
Any ideas about the counting up in twos?
Regards
James -
[quote author="jastmc" date="1366645117"]Hi,
Sorry but being new to Qt, I don't understand what you mean by
[quote]You shadowed both your Dialog timer variable in the constructor so it’s no initialized.
[/quote]Can you explain or point me to something where I can learn about this?
[/quote]