Qt 觸控屏中單點手勢的實現
阿新 • • 發佈:2021-12-10
單點觸控Qt會將其轉化為滑鼠事件,實現如下
https://www.dazhuanlan.com/mrwhooo/topics/1240549
但使用時發現,如果物件本身需要銷燬,則重新初始化物件時可能導致建構函式中的QGestureRecognizer::registerRecognizer 的多次重複註冊,進而引發崩潰,
崩潰堆疊3個“??”,查了一下午,析構時反註冊即可。
標頭檔案:
class GraphicsView : public QGraphicsView { Q_OBJECT public: GraphicsView(QWidget* parent = nullptr); ~GraphicsView() override; private: SwipeGestureRecognizer gesRecognizer_; Qt::GestureType gesType_; }
實現:
GraphicsView::GraphicsView(QWidget* parent) : QGraphicsView (parent) { gesType_= QGestureRecognizer::registerRecognizer(&gesRecognizer_); grabGesture(gesType_); } GraphicsView::~GraphicsView() { QGestureRecognizer::unregisterRecognizer(gesType_); ungrabGesture(gesType_); }