在具有实现的tabletEvent(QTabletEvent * event)和mousePressEvent(QMouseEvent * event)的QWidget派生类对象中,每次使用TabletEvent::TabletPress类型调用tabletEvent时,都会调用mousePressEvent。根据Qt documentation,这不应发生:
主窗口
#include "mainwindow.h"
#include "tabletwidget.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
TabletWidget* tw = new TabletWidget(this);
setCentralWidget(tw);
}
tabletwidget.h
#ifndef TABLETWIDGET_H
#define TABLETWIDGET_H
#include <QWidget>
class TabletWidget : public QWidget
{
Q_OBJECT
public:
explicit TabletWidget(QWidget *parent = 0);
protected:
void tabletEvent(QTabletEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
signals:
public slots:
};
#endif // TABLETWIDGET_H
tabletwidget.cpp
#include "tabletwidget.h"
#include <QDebug>
#include <QTabletEvent>
TabletWidget::TabletWidget(QWidget *parent) : QWidget(parent)
{
}
void TabletWidget::tabletEvent(QTabletEvent *event)
{
event->accept();
qDebug() << "tabletEvent: " << event->type();
}
void TabletWidget::mousePressEvent(QMouseEvent *event)
{
qDebug() << "mousePressEvent";
}
如果我使用笔尖或按Wacom Intuos CTH-680S-DEIT的任何按钮,则生成的输出为:
tabletEvent: 92
mousePressEvent
tabletEvent: 87
tabletEvent: 87
tabletEvent: 87
tabletEvent: 87
tabletEvent: 93
因此,首先会调用tabletEvent,即使我接受该事件,无论如何也会调用mousePressEvent。接下来的每个tabletEvent的类型都是QTabletEvent::TabletMove,最后一个是QTabletEvent::TabletRelease。从Qt documentation:
QEvent::TabletMove 87
QEvent::TabletPress 92
QEvent::TabletRelease 93
我已经在Mac OS 10.10.3和Windows 7上进行了测试,结果相同。这是错误还是我做错了?
这在Qt 5.4.2上进行了测试。
最佳答案
实际上,根据Qt文档,使用数位板时,Qt不应发送鼠标事件。但这似乎还是可以做到的(我使用的是5.5版)。
解决该问题的一种方法是重新实现event()
的QApplication
方法-这是TabletEnterProximity
和TabletLeaveProximity
的发送地址;这些函数不会发送到QWidget
的event()
。
因此,每当应用程序捕获TabletEnterProximity
或TabletLeaveProximity
事件时,您都可以向TabletWidget
发送信号以更改私有(private) bool(boolean) 变量_deviceActive
。然后,在TabletWidget
内部,为每个MousePressEvent
(和MouseReleaseEvent
)添加一个检查,以查看_deviceActive
是否为true;并仅在标志为false时实现事件。
为了说明这一点,继承的TabletApplication
如下所示:
class TabletApplication : public QApplication {
Q_OBJECT
public:
TabletApplication(int& argv, char** argc): QApplication(argv,argc){}
bool event(QEvent* event){
if (event->type() == QEvent::TabletEnterProximity || event->type() == QEvent::TabletLeaveProximity) {
bool active = event->type() == QEvent::TabletEnterProximity? 1:0;
emit sendTabletDevice(active);
return true;
}
return QApplication::event(event);
}
signals:
void sendTabletActive(bool active);
};
以及
tabletwidget.h
内的其他部分:class TabletWidget : public QWidget {
// ...
public slots:
void setTabletDeviceActive(bool active){
_deviceActive = active;
}
// ...
private:
bool _deviceActive;
};
然后在鼠标事件中检查设备是否处于 Activity 状态:
void TabletWidget::mousePressEvent(QMouseEvent *event)
{
if (!_deviceActive)
qDebug() << "mousePressEvent";
}
当然,不要忘记将相应的信号连接到插槽。希望能帮助到你。
引用:TabletApplication from Qt tablet example