大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
在触摸设备上可以使用Qt的手势事件
要激活手势事件,需要执行以下操作:
第一步,为QWidget控件注册手势事件
QList<Qt::GestureType> gestures;
gestures << Qt::PanGesture;
gestures << Qt::PinchGesture;
gestures << Qt::SwipeGesture;
gestures << Qt::TapGesture;
gestures << Qt::TapAndHoldGesture;
MainWindow w;
w.grabGestures(gestures);
第二步,在QWidget控件类中过滤事件
bool MainWindow::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent*>(event));
return QWidget::event(event);
}
bool MainWindow::gestureEvent(QGestureEvent *event)
{
qCDebug(lcExample) << "gestureEvent():" << event->gestures().size();
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
swipeTriggered(static_cast<QSwipeGesture *>(swipe));
else if (QGesture *pan = event->gesture(Qt::PanGesture))
panTriggered(static_cast<QPanGesture *>(pan));
else if (QGesture *tap = event->gesture(Qt::TapGesture))
tapTriggered(static_cast<QTapGesture *>(tap));
else if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
tapAndHoldTriggered(static_cast<QTapAndHoldGesture *>(tapAndHold));
if (QGesture *pinch = event->gesture(Qt::PinchGesture))
pinchTriggered(static_cast<QPinchGesture *>(pinch));
return true;
}
//! [gesture event handler]
void MainWindow::panTriggered(QPanGesture *gesture)
{
#ifndef QT_NO_CURSOR
switch (gesture->state()) {
case Qt::GestureStarted:
case Qt::GestureUpdated:
setCursor(Qt::SizeAllCursor);
break;
default:
setCursor(Qt::ArrowCursor);
}
#endif
QPointF delta = gesture->delta();
qCDebug(lcExample) << "panTriggered():" << delta;
horizontalOffset += delta.x();
verticalOffset += delta.y();
}
void MainWindow::pinchTriggered(QPinchGesture *gesture)
{
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
if (changeFlags & QPinchGesture::RotationAngleChanged) {
const qreal value = gesture->property("rotationAngle").toReal();
const qreal lastValue = gesture->property("lastRotationAngle").toReal();
const qreal rotationAngleDelta = value - lastValue;
rotationAngle += rotationAngleDelta;
qCDebug(lcExample) << "pinchTriggered(): rotation by" << rotationAngleDelta << rotationAngle;
}
if (changeFlags & QPinchGesture::ScaleFactorChanged) {
qreal value = gesture->property("scaleFactor").toReal();
currentStepScaleFactor = value;
qCDebug(lcExample) << "pinchTriggered(): " << currentStepScaleFactor;
}
if (gesture->state() == Qt::GestureFinished) {
scaleFactor *= currentStepScaleFactor;
currentStepScaleFactor = 1;
}
update();
}
//! [swipe function]
void MainWindow::swipeTriggered(QSwipeGesture *gesture)
{
if (gesture->state() == Qt::GestureFinished) {
if (gesture->horizontalDirection() == QSwipeGesture::Left
|| gesture->verticalDirection() == QSwipeGesture::Up) {
qCDebug(lcExample) << "swipeTriggered(): swipe to previous";
} else {
qCDebug(lcExample) << "swipeTriggered(): swipe to next";
}
}
}
//! [swipe function]
void MainWindow::tapTriggered(QTapGesture *gesture)
{
qCDebug(lcExample) << "tapTriggered():" ;
}
void MainWindow::tapAndHoldTriggered(QTapAndHoldGesture *gesture)
{
qCDebug(lcExample) << "tapAndHoldTriggered():";
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/197398.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...