c++ - mousePressEvent of QWidget gets called even though QTabletEvent was accepted -


in qwidget derived class object implemented tabletevent(qtabletevent *event) , mousepressevent(qmouseevent *event), mousepressevent gets called every time tabletevent gets called type tabletevent::tabletpress. according qt documentation, should not happen:

the event handler qwidget::tabletevent() receives tabletpress, tabletrelease , tabletmove events. qt first send tablet event, if not accepted widget, send mouse event.

mainwindow.cpp

#include "mainwindow.h" #include "tabletwidget.h"  mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent) {     tabletwidget* tw = new tabletwidget(this);     setcentralwidget(tw); } 

tabletwidget.h

#ifndef tabletwidget_h #define tabletwidget_h  #include <qwidget>  class tabletwidget : public qwidget {     q_object public:     explicit tabletwidget(qwidget *parent = 0);  protected:     void tabletevent(qtabletevent *event) q_decl_override;     void mousepressevent(qmouseevent *event) q_decl_override;  signals:  public slots: };  #endif // tabletwidget_h 

tabletwidget.cpp

#include "tabletwidget.h" #include <qdebug> #include <qtabletevent>  tabletwidget::tabletwidget(qwidget *parent) : qwidget(parent) {  }  void tabletwidget::tabletevent(qtabletevent *event) {     event->accept();     qdebug() << "tabletevent: " << event->type(); }  void tabletwidget::mousepressevent(qmouseevent *event) {     qdebug() << "mousepressevent"; } 

the output generated if use tip of pen or press button of wacom intuos cth-680s-deit is:

tabletevent:  92 mousepressevent tabletevent:  87 tabletevent:  87 tabletevent:  87 tabletevent:  87 tabletevent:  93 

so first tabletevent gets called, , though accept event, mousepressevent gets called anyway. every following tabletevent of type qtabletevent::tabletmove , last 1 qtabletevent::tabletrelease. qt documentation:

qevent::tabletmove 87 qevent::tabletpress 92 qevent::tabletrelease 93 

i have tested on mac os 10.10.3 , windows 7 same result. bug or doing wrong?

this tested on qt 5.4.2.

indeed, according qt documentation, qt should not sending mouse events when tablet in use. seems anyway (i'm using version 5.5).

one way around reimplement event() method of qapplication - that's tabletenterproximity , tabletleaveproximity sent; functions not sent qwidget's event().

so, whenever application catches either tabletenterproximity or tabletleaveproximity events, may send signal tabletwidget change private bool variable _deviceactive. then, inside tabletwidget add check each mousepressevent (and mousereleaseevent) see if _deviceactive true or not; , implement event if flag false.

to illustrate, inherited tabletapplication this:

class tabletapplication : public qapplication {     q_object public:     tabletapplication(int& argv, char** argc): qapplication(argv,argc){}     bool event(qevent* event){         if (event->type() == qevent::tabletenterproximity || event->type() == qevent::tabletleaveproximity) {             bool active = event->type() == qevent::tabletenterproximity? 1:0;             emit sendtabletdevice(active);             return true;          }         return qapplication::event(event); } signals:     void sendtabletactive(bool active); }; 

and additional parts inside tabletwidget.h:

class tabletwidget : public qwidget { // ... public slots:      void settabletdeviceactive(bool active){           _deviceactive = active;      } // ... private:      bool _deviceactive; }; 

then check inside mouse events if device active:

void tabletwidget::mousepressevent(qmouseevent *event) {      if (!_deviceactive)           qdebug() << "mousepressevent"; } 

of course, don't forget connect corresponding signal slot. hope helps.

reference: tabletapplication qt tablet example


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -