58.tablewidget模擬手指實現滑動

1.介紹html

嵌入式因爲須要支持手指滑動,因此先寫個demo,來試驗.post

每次按下的時候,獲取一次按下的pos以及按下的時間,而後釋放的時候獲取一次釋放pos,從而計算出,每秒移動的像素點,其中計算代碼以下所示:學習

int ms= QDateTime::currentDateTime().toMSecsSinceEpoch()-pressMSec;
int Pixel_per_second=qAbs(releasePoint_y - pressPoint_y)*1000/ms;       //計算每秒移動像素點

獲取到每秒移動像素點後,再結合ms(持續時間),進行判斷,從而實現手指離開後,是否須要再滑動一下.具體代碼以下所示:動畫

 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;
    }
}

最後再調用QPropertyAnimation類來實現動畫滑動:ui

animation->setDuration(2000-ms);
animation->setEndValue(moveValue);
animation->setEasingCurve(QEasingCurve::OutQuart);

界面以下圖所示:this

 

2.CustomScroll類url

CustomScroll:自定義滑動,該類包含了一個顯示滑動條.邏輯以下所示:spa

  • 當用戶只是單擊item時,則不顯示.
  • 若是用戶點擊item進行滑動時,則顯示.
  • 若是用戶滑動後釋放鼠標(離開手指),則1s後取消顯示

效果以下所示:.net

CustomScroll.h以下所示:3d

#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,                       //鼠標離開
        MOUSE_PRESS = 1,                         //按下
        MOUSE_PRESS_MOVE = 2,                   //按下移動
        MOUSE_RELEASE_MOVE = 3                   //鼠標離開並滑動
    }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);            //將滑動條移至最右側
    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 ();

     //根據鼠標的動做——按下、放開、拖動,執行相應的操做
    if(mouse)
    {
        if( mouse->type() ==QEvent::MouseButtonPress)    //首次按下
        {
            pressMSec = QDateTime::currentDateTime().toMSecsSinceEpoch();     //記錄按下的時間
            dragPoint_y  = mouse->pos().y();               //當前座標
            pressPoint_y = dragPoint_y;                      //按下的位置

            animation->stop();
            m_selectRow =  m_table->indexAt(mouse->pos() ).row();    //選擇當前行
            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;       //計算每秒像素點

            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())                         //已經移動了,因此取消選擇
                       m_selectTimer.stop();
                   m_table->clearSelection();
                   dragPoint_y = mouse->pos().y();                      //獲取當前座標
                   update();
                   return true;
                }
            }

            int moveValue = ( dragPoint_y-mouse->pos().y())+m_scrollBar->value();    //差距

            dragPoint_y = mouse->pos().y();                        //獲取當前座標

            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://download.csdn.net/download/qq_37997682/13614035

 未完,下章學習61.QT-QScroller實現home界面滑動效果

相關文章
相關標籤/搜索