当前位置:   article > 正文

58.tablewidget模拟手指实现滑动_根据鼠标的动作——按下、放开、拖动,执行相应的操作

根据鼠标的动作——按下、放开、拖动,执行相应的操作

1.介绍

嵌入式由于需要支持手指滑动,所以先写个demo,来试验.

每次按下的时候,获取一次按下的pos以及按下的时间,然后释放的时候获取一次释放pos,从而计算出,每秒移动的像素点,其中计算代码如下所示:

  1. int ms= QDateTime::currentDateTime().toMSecsSinceEpoch()-pressMSec;
  2. int Pixel_per_second=qAbs(releasePoint_y - pressPoint_y)*1000/ms; //计算每秒移动像素点

获取到每秒移动像素点后,再结合ms(持续时间),进行判断,从而实现手指离开后,是否需要再滑动一下.具体代码如下所示:

  1. if(ms > 1000) //滑动的时间太长
  2. {
  3. m_dragFlag = MOUSE_RELEASE;
  4. if(!m_scrollTimer.isActive())
  5. m_scrollTimer.start(1000); //1S后取消滑动条显示
  6. return true;
  7. }
  8. if(releasePoint_y - pressPoint_y > 0) //向下滑动
  9. {
  10. moveValue = m_scrollBar->value() - Pixel_per_second*0.2*(300/ms);//滑动时间越长,moveValue值越小,因为不是快速滑动
  11. if(moveValue < scrollV_min)
  12. {
  13. moveValue = scrollV_min;
  14. }
  15. }
  16. else
  17. {
  18. moveValue = m_scrollBar->value() + Pixel_per_second*0.2*(300/ms);
  19. if(moveValue > scrollV_max)
  20. {
  21. moveValue = scrollV_max;
  22. }
  23. }

最后再调用QPropertyAnimation类来实现动画滑动:

  1. animation->setDuration(2000-ms);
  2. animation->setEndValue(moveValue);
  3. animation->setEasingCurve(QEasingCurve::OutQuart);

界面如下图所示:

2.CustomScroll类

CustomScroll:自定义滑动,该类包含了一个显示滑动条.逻辑如下所示:

  • 当用户只是单击item时,则不显示.
  • 如果用户点击item进行滑动时,则显示.
  • 如果用户滑动后释放鼠标(离开手指),则1s后取消显示

效果如下所示:

CustomScroll.h如下所示:

  1. #ifndef CustomScroll_H
  2. #define CustomScroll_H
  3. #include <QObject>
  4. #include <QWidget>
  5. #include <QTimer>
  6. #include <QTableView>
  7. #include <QPropertyAnimation>
  8. #include <QDateTime>
  9. class CustomScroll : public QWidget
  10. {
  11. Q_OBJECT
  12. typedef enum tagLuiScrollMouseDragInfo {
  13. MOUSE_RELEASE = 0, //鼠标离开
  14. MOUSE_PRESS = 1, //按下
  15. MOUSE_PRESS_MOVE = 2, //按下移动
  16. MOUSE_RELEASE_MOVE = 3 //鼠标离开并滑动
  17. }LUI_Scroll_Mouse_Drag_INFO_E;
  18. LUI_Scroll_Mouse_Drag_INFO_E m_dragFlag = MOUSE_RELEASE;
  19. QTimer m_scrollTimer;
  20. QTimer m_selectTimer;
  21. QTableView *m_table;
  22. QScrollBar *m_scrollBar;
  23. QPropertyAnimation *animation;
  24. int m_selectRow;
  25. int m_srcollH;
  26. void paintEvent(QPaintEvent *);
  27. bool eventFilter(QObject *obj, QEvent *evt);
  28. public:
  29. explicit CustomScroll(QTableView* table,QWidget *parent = nullptr);
  30. signals:
  31. public slots:
  32. void scrollTimeOut();
  33. void selectTimeOut();
  34. };
  35. #endif // CustomScroll_H

CustomScroll.cpp如下所示:

  1. #include "customscroll.h"
  2. #include <QMouseEvent>
  3. #include <QDebug>
  4. #include <QApplication>
  5. #include <QPainter>
  6. #include <QTableWidget>
  7. #include <QHeaderView>
  8. #include <QScrollBar>
  9. #include <QAbstractAnimation>
  10. CustomScroll::CustomScroll(QTableView* table,QWidget *parent) : QWidget(parent)
  11. {
  12. #define SRCOLL_HEIGHT 22
  13. setAttribute(Qt::WA_TranslucentBackground);
  14. m_table = table;
  15. m_scrollBar = table->verticalScrollBar();
  16. m_table->viewport()->installEventFilter(this);
  17. m_table->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
  18. animation = new QPropertyAnimation(m_scrollBar,"value",this);
  19. connect(&m_scrollTimer,SIGNAL(timeout()),this,SLOT(scrollTimeOut()));
  20. connect(&m_selectTimer,SIGNAL(timeout()),this,SLOT(selectTimeOut()));
  21. this->setMinimumSize(10, table->height());
  22. this->setMaximumSize(10, table->height());
  23. this->move(table->width()-10,0); //将滑动条移至最右侧
  24. this->raise();
  25. m_srcollH = table->height()* SRCOLL_HEIGHT/100.0;
  26. }
  27. void CustomScroll::selectTimeOut()
  28. {
  29. m_table->selectRow(m_selectRow);
  30. m_selectTimer.stop();
  31. this->update();
  32. }
  33. void CustomScroll::scrollTimeOut()
  34. {
  35. if(m_dragFlag == MOUSE_RELEASE_MOVE && animation->state()==QAbstractAnimation::Stopped) //停下来了
  36. {
  37. this->update();
  38. m_dragFlag = MOUSE_RELEASE;
  39. m_scrollTimer.setInterval(1000);
  40. }
  41. else
  42. {
  43. this->update();
  44. if(m_scrollTimer.interval()==1000)
  45. m_scrollTimer.stop();
  46. }
  47. }
  48. bool CustomScroll::eventFilter(QObject *obj, QEvent *evt)
  49. {
  50. static int pressPoint_y = 0;
  51. static int dragPoint_y = -1;
  52. static qint64 pressMSec ;
  53. QMouseEvent *mouse = dynamic_cast<QMouseEvent *>(evt);
  54. int scrollV_max = m_scrollBar->maximum ();
  55. int scrollV_min = m_scrollBar->minimum ();
  56. //根据鼠标的动作——按下、放开、拖动,执行相应的操作
  57. if(mouse)
  58. {
  59. if( mouse->type() ==QEvent::MouseButtonPress) //首次按下
  60. {
  61. pressMSec = QDateTime::currentDateTime().toMSecsSinceEpoch(); //记录按下的时间
  62. dragPoint_y = mouse->pos().y(); //当前坐标
  63. pressPoint_y = dragPoint_y; //按下的位置
  64. animation->stop();
  65. m_selectRow = m_table->indexAt(mouse->pos() ).row(); //选择当前行
  66. qDebug()<<mouse->pos()<<m_selectRow;
  67. m_selectTimer.start(100);
  68. m_dragFlag = MOUSE_PRESS;
  69. return true;
  70. }
  71. else if(mouse->type() == QEvent::MouseButtonRelease && m_dragFlag == MOUSE_PRESS) //未移动
  72. {
  73. m_dragFlag = MOUSE_RELEASE;
  74. if(!m_scrollTimer.isActive())
  75. m_scrollTimer.start(1000); //1S后取消滑动条显示
  76. return true;
  77. }
  78. else if(mouse->type() == QEvent::MouseButtonRelease && m_dragFlag == MOUSE_PRESS_MOVE)
  79. {
  80. dragPoint_y = -1;
  81. int releasePoint_y = mouse->pos().y();
  82. int ms= QDateTime::currentDateTime().toMSecsSinceEpoch()-pressMSec;
  83. int Pixel_per_second=qAbs(releasePoint_y - pressPoint_y)*1000/ms; //计算每秒像素点
  84. if(Pixel_per_second<300 || qAbs(releasePoint_y - pressPoint_y) < 45)
  85. {
  86. m_dragFlag = MOUSE_RELEASE;
  87. if(!m_scrollTimer.isActive())
  88. m_scrollTimer.start(1000); //1S后取消滑动条显示
  89. return true;
  90. }
  91. else
  92. {
  93. int moveValue ;
  94. if(ms > 1000) //滑动的时间太长
  95. {
  96. m_dragFlag = MOUSE_RELEASE;
  97. if(!m_scrollTimer.isActive())
  98. m_scrollTimer.start(1000); //1S后取消滑动条显示
  99. return true;
  100. }
  101. if(releasePoint_y - pressPoint_y > 0) //向下滑动
  102. {
  103. moveValue = m_scrollBar->value() - Pixel_per_second*0.2*(300/ms);//滑动时间越长,moveValue值越小,因为不是快速滑动
  104. if(moveValue < scrollV_min)
  105. {
  106. moveValue = scrollV_min;
  107. }
  108. }
  109. else
  110. {
  111. moveValue = m_scrollBar->value() + Pixel_per_second*0.2*(300/ms);
  112. if(moveValue > scrollV_max)
  113. {
  114. moveValue = scrollV_max;
  115. }
  116. }
  117. animation->setDuration(2000-ms);
  118. animation->setEndValue(moveValue);
  119. animation->setEasingCurve(QEasingCurve::OutQuart);
  120. if(!m_scrollTimer.isActive())
  121. m_scrollTimer.start(50); //定时刷新滑动条显示
  122. animation->start();
  123. m_dragFlag = MOUSE_RELEASE_MOVE;
  124. }
  125. return true;
  126. }
  127. else if(mouse->type() == QEvent::MouseMove && (m_dragFlag!= MOUSE_RELEASE) )
  128. {
  129. if( m_dragFlag == MOUSE_PRESS) //开始移动
  130. {
  131. if(qAbs(dragPoint_y - mouse->pos().y()) < 4) //判断移动阀值,避免误操作
  132. return true;
  133. else
  134. {
  135. m_dragFlag = MOUSE_PRESS_MOVE;
  136. if(m_selectTimer.isActive()) //已经移动了,所以取消选择
  137. m_selectTimer.stop();
  138. m_table->clearSelection();
  139. dragPoint_y = mouse->pos().y(); //获取当前坐标
  140. update();
  141. return true;
  142. }
  143. }
  144. int moveValue = ( dragPoint_y-mouse->pos().y())+m_scrollBar->value(); //差距
  145. dragPoint_y = mouse->pos().y(); //获取当前坐标
  146. if(scrollV_min > moveValue)
  147. {
  148. moveValue = scrollV_min;
  149. }
  150. if(moveValue > scrollV_max)
  151. {
  152. moveValue = scrollV_max;
  153. }
  154. m_scrollBar->setValue(moveValue);
  155. update();
  156. return true;
  157. }
  158. }
  159. return QWidget::eventFilter(obj,evt);
  160. }
  161. void CustomScroll::paintEvent(QPaintEvent *)
  162. {
  163. #define WIDTH 6
  164. #define MIN_HEIGHT 6
  165. if(m_dragFlag== MOUSE_RELEASE||m_dragFlag== MOUSE_PRESS)
  166. {
  167. return;
  168. }
  169. int scrollV_max = m_scrollBar->maximum ();
  170. QPainter painter(this);
  171. int y = (m_table->verticalScrollBar()->value()*(m_table->height()-m_srcollH))/(float)(scrollV_max);
  172. painter.setPen(Qt::NoPen);
  173. // painter.setBrush(QColor(180,180,180,200));
  174. // painter.drawRoundedRect(0,0,this->width(),this->height(),3,3);
  175. painter.setBrush(QColor(80,80,80,140));
  176. painter.drawRoundedRect(0,y,WIDTH,m_srcollH,3,3);
  177. }

 资源下载地址: Qtablewidget和Qtableview实现手指滑动_qtablewidget和qtableview,qtableview滑动-嵌入式文档类资源-CSDN下载

 未完,下章学习 61.QT-QSortFilterProxyModel代理实现自定义排序、联合过滤_诺谦的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/80667
推荐阅读
相关标签
  

闽ICP备14008679号