Solved How to cause a QTimer to start or stop upon a pushButton event?
-
Hello there!
I am new to Qt. Here i have written a simple code for changing a "Set" value inside a QLabel. All i want is to blink the label only when the arrow buttons are enabled. For the blink purpose, i have used a QTimer. But the blinking action is happening even when the arrow buttons are unavailable. So i am trying to control QTimer inside a slot method. By starting the timer inside a slot method, i am getting error as " 'blinker' : undeclared identifier". Kindly suggest proper approach for the purpose. Thank you.Project Folder
https://drive.google.com/open?id=1DeUyh77jxJWaNBSSrysftmOxB68ZPw3Y
*Environment:
Windows 8.1
Visual Studio 2008 SP1 trial with Qt 4.8.4 Plugin
Platform: Toradex_CE700 (ARMv4I)*
//main.cpp #include"mockhmigui.h" #include<QtGui/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); mockHMIGUI w; w.show(); return a.exec(); } //mockhmigui.h #ifndef MOCKHMIGUI_H #define MOCKHMIGUI_H #include <QtGui/QMainWindow> #include "ui_mockhmigui.h" class mockHMIGUI : public QMainWindow { Q_OBJECT public: mockHMIGUI(QWidget *parent = 0, Qt::WFlags flags = 0); ~mockHMIGUI(); private: Ui::mockHMIGUIClass ui; public slots: void on_btn_incrSet_clicked(); void on_btn_decrSet_clicked(); void on_btnSet_clicked(); void blink(); }; #endif // MOCKHMIGUI_H //mockhmigui.cpp #include "mockhmigui.h" #include<QtGui> static short setPoint = 370; static short count,blinkFlag; mockHMIGUI::mockHMIGUI(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { ui.setupUi(this); ui.btn_incrSet->setEnabled(false); ui.btn_decrSet->setEnabled(false); ui.labelSet->setNum(setPoint); QTimer *blinker = new QTimer(); connect(blinker, SIGNAL(timeout()), SLOT(blink())); blinker->start(800); } mockHMIGUI::~mockHMIGUI() {} /*****************************PUSHBUTTONS************************************/ void mockHMIGUI::on_btn_incrSet_clicked() // INCR { ui.labelSet->setNum(setPoint++); } void mockHMIGUI::on_btn_decrSet_clicked() // DECR { ui.labelSet->setNum(setPoint++); } void mockHMIGUI::on_btnSet_clicked() // SET { if(!(count%2)) { ui.btn_incrSet->setEnabled(true); ui.btn_decrSet->setEnabled(true); /*want to start blinker here*/ } else { ui.btn_incrSet->setEnabled(false); ui.btn_decrSet->setEnabled(false); /*want to stop blinker here*/ } count++; } /******************BLINK***************/ void mockHMIGUI::blink() { if(!(blinkFlag%2)) ui.labelSet->setText(""); else ui.labelSet->setNum(setPoint); blinkFlag++; }
-
@Ejay said in How to cause a QTimer to start or stop upon a pushButton event?:
'blinker' : undeclared identifier".
You should make your blinker a class member.
-
@Ejay said in How to cause a QTimer to start or stop upon a pushButton event?:
QTimer *blinker = new QTimer();
You declare it as a local variable only known in constructor
Make it a class member by moving
QTimer *blinker;
To the mockHMIGUI.h inside class
and then do
blinker = new QTimer();Then its available to all functions in mockHMIGUI
@dream_captain :) ninja'd
-
-
@dream_captain
Good catch :) -
@dream_captain @mrjj Your suggestions were helpful. Thanks a lot!