Qt 触控屏中单点手势的实现

单点触控Qt会将其转化为鼠标事件,实现如下

https://www.dazhuanlan.com/mrwhooo/topics/1240549

https://github.com/digifant/multidisplay-app/blob/b0921e1b0f0c728c58427029374bef072d0c4382/src/mobile/SwipeGestureRecognizer.h

但使用时发现,如果对象本身需要销毁,则重新初始化对象时可能导致构造函数中的 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_);
}

 

上一篇:Spring中的IOC和DI(“最易懂得Spring学习”)(四)


下一篇:2021-11-12