From 9678b6f6db35f6dd9376229ab5996ac876d74949 Mon Sep 17 00:00:00 2001 From: jixiaoxu Date: Mon, 5 Dec 2022 14:51:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DUI=E6=9C=80=E4=BD=B3=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E4=B8=8E=E7=BB=93=E6=9E=9C=E5=88=86=E7=B1=BB=E8=A7=A6?= =?UTF-8?q?=E6=91=B8=E8=A1=8C=E4=B8=BA=E4=B8=8D=E4=B8=80=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/view/best-list-view.cpp | 53 +++++++++++++++++++++++++++++++- frontend/view/best-list-view.h | 2 ++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/frontend/view/best-list-view.cpp b/frontend/view/best-list-view.cpp index 547689f..4f6d56a 100644 --- a/frontend/view/best-list-view.cpp +++ b/frontend/view/best-list-view.cpp @@ -10,6 +10,7 @@ BestListView::BestListView(QWidget *parent) : QTreeView(parent) { setStyle(ResultItemStyle::getStyle()); this->setFrameShape(QFrame::NoFrame); + this->viewport()->setAttribute(Qt::WA_AcceptTouchEvents); this->viewport()->setAutoFillBackground(false); this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE)); this->setRootIsDecorated(false); @@ -22,6 +23,9 @@ BestListView::BestListView(QWidget *parent) : QTreeView(parent) initConnections(); m_styleDelegate = new ResultViewDelegate(this); this->setItemDelegate(m_styleDelegate); + m_touchTimer = new QTimer(this); + m_touchTimer->setSingleShot(true); + m_touchTimer->setInterval(100); } bool BestListView::isSelected() @@ -185,12 +189,59 @@ void BestListView::mouseMoveEvent(QMouseEvent *event) { m_tmpCurrentIndex = this->currentIndex(); m_tmpMousePressIndex = indexAt(event->pos()); - if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex) { + if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex and event->source() != Qt::MouseEventSynthesizedByQt) { Q_EMIT this->clicked(m_tmpMousePressIndex); } return QTreeView::mouseMoveEvent(event); } +bool BestListView::viewportEvent(QEvent *event) +{ + if (event->type() == QEvent::TouchBegin) { + qDebug() << "TouchBegin=============="; + QTouchEvent *e = dynamic_cast(event); + QMouseEvent me(QEvent::MouseButtonPress, + e->touchPoints().at(0).pos(), + this->mapTo(this->window(),e->touchPoints().at(0).pos().toPoint()), + this->mapToGlobal(e->touchPoints().at(0).pos().toPoint()), + Qt::LeftButton,Qt::LeftButton,Qt::NoModifier,Qt::MouseEventSynthesizedByApplication); + QApplication::sendEvent(parent(), &me); + m_touchTimer->start(); + event->accept(); + return true; + } else if (event->type() == QEvent::TouchEnd) { + qDebug() << "touchend==============" << m_touchTimer->remainingTime(); + if (m_touchTimer->remainingTime() > 0.001) { + QTouchEvent *e = dynamic_cast(event); + QMouseEvent me(QEvent::MouseButtonPress, + e->touchPoints().at(0).pos(), + this->mapTo(this->window(),e->touchPoints().at(0).pos().toPoint()), + this->mapToGlobal(e->touchPoints().at(0).pos().toPoint()), + Qt::LeftButton,Qt::LeftButton,Qt::NoModifier,Qt::MouseEventSynthesizedByApplication); + QApplication::sendEvent(this->viewport(),&me); + + QMouseEvent mer(QEvent::MouseButtonRelease, + e->touchPoints().at(0).pos(), + this->mapTo(this->window(),e->touchPoints().at(0).pos().toPoint()), + this->mapToGlobal(e->touchPoints().at(0).pos().toPoint()), + Qt::LeftButton,Qt::LeftButton,Qt::NoModifier,Qt::MouseEventSynthesizedByApplication); + QApplication::sendEvent(this->viewport(),&mer); + } + return true; + } else if (event->type() == QEvent::TouchUpdate) { + qDebug() << "touchupdate=============="; + QTouchEvent *e = dynamic_cast(event); + QMouseEvent me(QEvent::MouseMove, + e->touchPoints().at(0).pos(), + this->mapTo(this->window(),e->touchPoints().at(0).pos().toPoint()), + this->mapToGlobal(e->touchPoints().at(0).pos().toPoint()), + Qt::LeftButton,Qt::LeftButton,Qt::NoModifier,Qt::MouseEventSynthesizedByApplication); + QApplication::sendEvent(parent(), &me); + return true; + } + return QTreeView::viewportEvent(event); +} + void BestListView::initConnections() { connect(this, &BestListView::startSearch, [ = ](const QString &keyword) { diff --git a/frontend/view/best-list-view.h b/frontend/view/best-list-view.h index b38a23d..75c8a04 100644 --- a/frontend/view/best-list-view.h +++ b/frontend/view/best-list-view.h @@ -40,6 +40,7 @@ protected: void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent * event); void mouseMoveEvent(QMouseEvent *event); + bool viewportEvent(QEvent *event); private: void initConnections(); @@ -50,6 +51,7 @@ private: int m_count = 0; QModelIndex m_tmpCurrentIndex; QModelIndex m_tmpMousePressIndex; + QTimer *m_touchTimer; Q_SIGNALS: void startSearch(const QString &);