修复UI最佳匹配与结果分类触摸行为不一致的问题;

This commit is contained in:
jixiaoxu 2022-12-05 14:51:57 +08:00 committed by iaom
parent 60fdf07549
commit 9678b6f6db
2 changed files with 54 additions and 1 deletions

View File

@ -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<QTouchEvent *>(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<QTouchEvent *>(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<QTouchEvent *>(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) {

View File

@ -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 &);