2

我正在尝试在这个基于 QWidget 的简单程序中处理多点触控事件,但无法接收任何触控事件。

"MyWidget.h"

#include <QWidget>
class QPaintEvent;
class QEvent;

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent *);
    bool event ( QEvent * event );


};


"MyWidget.cpp"

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    setAttribute(Qt::WA_AcceptTouchEvents);
}

void MyWidget::paintEvent(QPaintEvent *evt) {

    QPainter painter(this);
    painter.fillRect(rect(),QColor(0,255,0));
  //  painter.drawText(QPoint(rect().left(),rect().top()),"Hello world");
}

bool MyWidget::event(QEvent *event){
    if(event->type() == QEvent::TouchBegin ||
            event->type() == QEvent::TouchEnd ||
            event->type() == QEvent::TouchUpdate ){

        qDebug() <<"Touch events";
    }
    else if(event->type() == QEvent::MouseButtonDblClick) {
        qDebug() <<"double click";

    }

    return QWidget::event(event);
}

我在这里错过了什么吗?

4

1 回答 1

0

要使触摸事件起作用,请将以下内容添加到您的 MainWindow:

MyWidget *myWidget = ...;
setCentralWidget(myWidget);

在 MyWidget 构造函数中添加:

setAttribute(Qt::WA_AcceptTouchEvents);
//grabGesture(Qt::PinchGesture);
//setAttribute(Qt::WA_InputMethodEnabled);
//setFocusPolicy(Qt::WheelFocus);
setAttribute(Qt::WA_StaticContents);
于 2014-04-24T05:33:10.943 回答