c++ - Qt handling QPushButton Autorepeat() behavior with isAutoRepeat() -
i'm new qt , making widget interfaces pre-existing gui. i'd have widget continuously output 1 signal while user has pushbutton pressed , continuously output when released.
by enabling autorepeat can have widget output signal while user pressing pushbutton, however, output signal switches between pressed() , released(). e.g.
<> outputs: * pressed signal * released signal * pressed signal * released signal
i've seen question been asked keypressevents i'm not sure how access isautorepeat() pushbuttons. can give me advice on this?
one way can use timer object achieve this. below example, run 2 slot's when button pressed , released. code comment explain in detail. when button pressed & released text box show continuous time in milli-seconds. timer object emit timeout() signal in given interval. need stop , start alternate timers in button pressed / released signal. application created using qt creator "qt widgets application" wizard. hope help.
//header file
class mainwindow : public qmainwindow{ q_object public: explicit mainwindow(qwidget *parent = 0); ~mainwindow(); private slots: //button slots void on_pushbutton_pressed(); //continuous press void on_pushbutton_released(); //continuous release void on_pushbutton_2_clicked(); //stop both timer //qtimer timeout actions void timer1_action(); void timer2_action(); private: ui::mainwindow *ui; //timer object qtimer *t1, *t2; //date time object testing qdatetime dt1,dt2; };
//cpp file
mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow){ ui->setupui(this); //parent object take care of deallocation of 2 timer objects t1 = new qtimer(this); t2 = new qtimer(this); //interval timer object t1->setinterval(10); t2->setinterval(10); //signal slot timer this->connect(t1,signal(timeout()),this,slot(timer1_action())); this->connect(t2,signal(timeout()),this,slot(timer2_action())); } mainwindow::~mainwindow(){ delete ui; } void mainwindow::on_pushbutton_pressed(){ //starting , stoping timer t2->stop(); t1->start(); //date time when pressed dt1 = qdatetime::currentdatetime(); } void mainwindow::on_pushbutton_released(){ //starting , stoping timer t1->stop(); t2->start(); //date time when pressed dt2 = qdatetime::currentdatetime(); } void mainwindow::timer1_action(){ ui->txttimer1->setplaintext("button pressed " + qstring::number(dt1.msecsto(qdatetime::currentdatetime())) + " milli seconds"); } void mainwindow::timer2_action(){ ui->txttimer2->setplaintext("button released " + qstring::number(dt2.msecsto(qdatetime::currentdatetime())) + " milli seconds"); } void mainwindow::on_pushbutton_2_clicked(){ //stoping both timer t1->stop(); t2->stop(); }
Comments
Post a Comment