单点触控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_); }