1. 程式人生 > 其它 >Qt 觸控屏中單點手勢的實現

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_); }