为什么使用触摸板(笔记本电脑)的多点触控事件在这个Qt程序中不起作用?

时间:2013-06-11 10:44:46

标签: qt

我正在尝试在这个简单的基于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);
}

我在这里遗漏了什么吗?

1 个答案:

答案 0 :(得分: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);