網(wǎng)站首頁 編程語言 正文
1.介紹
嵌入式由于需要支持手指滑動,所以先寫個demo,來試驗.
每次按下的時候,獲取一次按下的pos以及按下的時間,然后釋放的時候獲取一次釋放pos,從而計算出,每秒移動的像素點(diǎn),其中計算代碼如下所示:
int ms= QDateTime::currentDateTime().toMSecsSinceEpoch()-pressMSec;
int Pixel_per_second=qAbs(releasePoint_y - pressPoint_y)*1000/ms; //計算每秒移動像素點(diǎn)
獲取到每秒移動像素點(diǎn)后,再結(jié)合ms(持續(xù)時間),進(jìn)行判斷,從而實現(xiàn)手指離開后,是否需要再滑動一下.具體代碼如下所示:
if(ms > 1000) //滑動的時間太長
{
m_dragFlag = MOUSE_RELEASE;
if(!m_scrollTimer.isActive())
m_scrollTimer.start(1000); //1S后取消滑動條顯示
return true;
}
if(releasePoint_y - pressPoint_y > 0) //向下滑動
{
moveValue = m_scrollBar->value() - Pixel_per_second*0.2*(300/ms);//滑動時間越長,moveValue值越小,因為不是快速滑動
if(moveValue < scrollV_min)
{
moveValue = scrollV_min;
}
}
else
{
moveValue = m_scrollBar->value() + Pixel_per_second*0.2*(300/ms);
if(moveValue > scrollV_max)
{
moveValue = scrollV_max;
}
}
最后再調(diào)用QPropertyAnimation類來實現(xiàn)動畫滑動:
animation->setDuration(2000-ms);
animation->setEndValue(moveValue);
animation->setEasingCurve(QEasingCurve::OutQuart);
界面如下圖所示:
2.CustomScroll類
CustomScroll:自定義滑動,該類包含了一個顯示滑動條.邏輯如下所示:
- 當(dāng)用戶只是單擊item時,則不顯示.
- 如果用戶點(diǎn)擊item進(jìn)行滑動時,則顯示.
- 如果用戶滑動后釋放鼠標(biāo)(離開手指),則1s后取消顯示
效果如下所示:
CustomScroll.h如下所示:
#ifndef CustomScroll_H
#define CustomScroll_H
#include <QObject>
#include <QWidget>
#include <QTimer>
#include <QTableView>
#include <QPropertyAnimation>
#include <QDateTime>
class CustomScroll : public QWidget
{
Q_OBJECT
typedef enum tagLuiScrollMouseDragInfo {
MOUSE_RELEASE = 0, //鼠標(biāo)離開
MOUSE_PRESS = 1, //按下
MOUSE_PRESS_MOVE = 2, //按下移動
MOUSE_RELEASE_MOVE = 3 //鼠標(biāo)離開并滑動
}LUI_Scroll_Mouse_Drag_INFO_E;
LUI_Scroll_Mouse_Drag_INFO_E m_dragFlag = MOUSE_RELEASE;
QTimer m_scrollTimer;
QTimer m_selectTimer;
QTableView *m_table;
QScrollBar *m_scrollBar;
QPropertyAnimation *animation;
int m_selectRow;
int m_srcollH;
void paintEvent(QPaintEvent *);
bool eventFilter(QObject *obj, QEvent *evt);
public:
explicit CustomScroll(QTableView* table,QWidget *parent = nullptr);
signals:
public slots:
void scrollTimeOut();
void selectTimeOut();
};
#endif // CustomScroll_H
CustomScroll.cpp如下所示:
#include "customscroll.h"
#include <QMouseEvent>
#include <QDebug>
#include <QApplication>
#include <QPainter>
#include <QTableWidget>
#include <QHeaderView>
#include <QScrollBar>
#include <QAbstractAnimation>
CustomScroll::CustomScroll(QTableView* table,QWidget *parent) : QWidget(parent)
{
#define SRCOLL_HEIGHT 22
setAttribute(Qt::WA_TranslucentBackground);
m_table = table;
m_scrollBar = table->verticalScrollBar();
m_table->viewport()->installEventFilter(this);
m_table->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
animation = new QPropertyAnimation(m_scrollBar,"value",this);
connect(&m_scrollTimer,SIGNAL(timeout()),this,SLOT(scrollTimeOut()));
connect(&m_selectTimer,SIGNAL(timeout()),this,SLOT(selectTimeOut()));
this->setMinimumSize(10, table->height());
this->setMaximumSize(10, table->height());
this->move(table->width()-10,0); //將滑動條移至最右側(cè)
this->raise();
m_srcollH = table->height()* SRCOLL_HEIGHT/100.0;
}
void CustomScroll::selectTimeOut()
{
m_table->selectRow(m_selectRow);
m_selectTimer.stop();
this->update();
}
void CustomScroll::scrollTimeOut()
{
if(m_dragFlag == MOUSE_RELEASE_MOVE && animation->state()==QAbstractAnimation::Stopped) //停下來了
{
this->update();
m_dragFlag = MOUSE_RELEASE;
m_scrollTimer.setInterval(1000);
}
else
{
this->update();
if(m_scrollTimer.interval()==1000)
m_scrollTimer.stop();
}
}
bool CustomScroll::eventFilter(QObject *obj, QEvent *evt)
{
static int pressPoint_y = 0;
static int dragPoint_y = -1;
static qint64 pressMSec ;
QMouseEvent *mouse = dynamic_cast<QMouseEvent *>(evt);
int scrollV_max = m_scrollBar->maximum ();
int scrollV_min = m_scrollBar->minimum ();
//根據(jù)鼠標(biāo)的動作——按下、放開、拖動,執(zhí)行相應(yīng)的操作
if(mouse)
{
if( mouse->type() ==QEvent::MouseButtonPress) //首次按下
{
pressMSec = QDateTime::currentDateTime().toMSecsSinceEpoch(); //記錄按下的時間
dragPoint_y = mouse->pos().y(); //當(dāng)前坐標(biāo)
pressPoint_y = dragPoint_y; //按下的位置
animation->stop();
m_selectRow = m_table->indexAt(mouse->pos() ).row(); //選擇當(dāng)前行
qDebug()<<mouse->pos()<<m_selectRow;
m_selectTimer.start(100);
m_dragFlag = MOUSE_PRESS;
return true;
}
else if(mouse->type() == QEvent::MouseButtonRelease && m_dragFlag == MOUSE_PRESS) //未移動
{
m_dragFlag = MOUSE_RELEASE;
if(!m_scrollTimer.isActive())
m_scrollTimer.start(1000); //1S后取消滑動條顯示
return true;
}
else if(mouse->type() == QEvent::MouseButtonRelease && m_dragFlag == MOUSE_PRESS_MOVE)
{
dragPoint_y = -1;
int releasePoint_y = mouse->pos().y();
int ms= QDateTime::currentDateTime().toMSecsSinceEpoch()-pressMSec;
int Pixel_per_second=qAbs(releasePoint_y - pressPoint_y)*1000/ms; //計算每秒像素點(diǎn)
if(Pixel_per_second<300 || qAbs(releasePoint_y - pressPoint_y) < 45)
{
m_dragFlag = MOUSE_RELEASE;
if(!m_scrollTimer.isActive())
m_scrollTimer.start(1000); //1S后取消滑動條顯示
return true;
}
else
{
int moveValue ;
if(ms > 1000) //滑動的時間太長
{
m_dragFlag = MOUSE_RELEASE;
if(!m_scrollTimer.isActive())
m_scrollTimer.start(1000); //1S后取消滑動條顯示
return true;
}
if(releasePoint_y - pressPoint_y > 0) //向下滑動
{
moveValue = m_scrollBar->value() - Pixel_per_second*0.2*(300/ms);//滑動時間越長,moveValue值越小,因為不是快速滑動
if(moveValue < scrollV_min)
{
moveValue = scrollV_min;
}
}
else
{
moveValue = m_scrollBar->value() + Pixel_per_second*0.2*(300/ms);
if(moveValue > scrollV_max)
{
moveValue = scrollV_max;
}
}
animation->setDuration(2000-ms);
animation->setEndValue(moveValue);
animation->setEasingCurve(QEasingCurve::OutQuart);
if(!m_scrollTimer.isActive())
m_scrollTimer.start(50); //定時刷新滑動條顯示
animation->start();
m_dragFlag = MOUSE_RELEASE_MOVE;
}
return true;
}
else if(mouse->type() == QEvent::MouseMove && (m_dragFlag!= MOUSE_RELEASE) )
{
if( m_dragFlag == MOUSE_PRESS) //開始移動
{
if(qAbs(dragPoint_y - mouse->pos().y()) < 4) //判斷移動閥值,避免誤操作
return true;
else
{
m_dragFlag = MOUSE_PRESS_MOVE;
if(m_selectTimer.isActive()) //已經(jīng)移動了,所以取消選擇
m_selectTimer.stop();
m_table->clearSelection();
dragPoint_y = mouse->pos().y(); //獲取當(dāng)前坐標(biāo)
update();
return true;
}
}
int moveValue = ( dragPoint_y-mouse->pos().y())+m_scrollBar->value(); //差距
dragPoint_y = mouse->pos().y(); //獲取當(dāng)前坐標(biāo)
if(scrollV_min > moveValue)
{
moveValue = scrollV_min;
}
if(moveValue > scrollV_max)
{
moveValue = scrollV_max;
}
m_scrollBar->setValue(moveValue);
update();
return true;
}
}
return QWidget::eventFilter(obj,evt);
}
void CustomScroll::paintEvent(QPaintEvent *)
{
#define WIDTH 6
#define MIN_HEIGHT 6
if(m_dragFlag== MOUSE_RELEASE||m_dragFlag== MOUSE_PRESS)
{
return;
}
int scrollV_max = m_scrollBar->maximum ();
QPainter painter(this);
int y = (m_table->verticalScrollBar()->value()*(m_table->height()-m_srcollH))/(float)(scrollV_max);
painter.setPen(Qt::NoPen);
// painter.setBrush(QColor(180,180,180,200));
// painter.drawRoundedRect(0,0,this->width(),this->height(),3,3);
painter.setBrush(QColor(80,80,80,140));
painter.drawRoundedRect(0,y,WIDTH,m_srcollH,3,3);
}
原文鏈接:https://blog.csdn.net/qq_37997682/article/details/119590513
相關(guān)推薦
- 2022-04-10 Python?十個字典用法使用技巧歸納_python
- 2022-09-14 Redis核心原理詳細(xì)解說_Redis
- 2022-11-12 asp.net?web?api2設(shè)置默認(rèn)啟動登錄頁面的方法_實用技巧
- 2022-03-28 用python實現(xiàn)一個文件搜索工具_(dá)python
- 2023-01-14 C++實現(xiàn)一個簡單消息隊列的示例詳解_C 語言
- 2022-04-16 Python實現(xiàn)杰卡德距離以及環(huán)比算法講解_python
- 2021-12-06 centos7.6批量增加修改刪除虛擬網(wǎng)卡操作介紹_Linux
- 2023-12-25 Spring 之 @Cacheable 緩存使用教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支