最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)

 更新時(shí)間:2023年01月14日 10:56:18   作者:諾謙  
這篇文章主要為大家詳細(xì)介紹了Qt如何利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定的幫助,需要的可以參考一下

1.介紹

嵌入式由于需要支持手指滑動(dòng),所以先寫個(gè)demo,來(lái)試驗(yàn).

每次按下的時(shí)候,獲取一次按下的pos以及按下的時(shí)間,然后釋放的時(shí)候獲取一次釋放pos,從而計(jì)算出,每秒移動(dòng)的像素點(diǎn),其中計(jì)算代碼如下所示:

int ms= QDateTime::currentDateTime().toMSecsSinceEpoch()-pressMSec;
int Pixel_per_second=qAbs(releasePoint_y - pressPoint_y)*1000/ms;       //計(jì)算每秒移動(dòng)像素點(diǎn)

獲取到每秒移動(dòng)像素點(diǎn)后,再結(jié)合ms(持續(xù)時(shí)間),進(jìn)行判斷,從而實(shí)現(xiàn)手指離開后,是否需要再滑動(dòng)一下.具體代碼如下所示:

 if(ms > 1000)      //滑動(dòng)的時(shí)間太長(zhǎng)
{
     m_dragFlag = MOUSE_RELEASE;
     if(!m_scrollTimer.isActive())
         m_scrollTimer.start(1000);  //1S后取消滑動(dòng)條顯示
     return true;
}
 
if(releasePoint_y - pressPoint_y > 0)     //向下滑動(dòng)
{
    moveValue = m_scrollBar->value() - Pixel_per_second*0.2*(300/ms);//滑動(dòng)時(shí)間越長(zhǎng),moveValue值越小,因?yàn)椴皇强焖倩瑒?dòng)
    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類來(lái)實(shí)現(xiàn)動(dòng)畫滑動(dòng):

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

界面如下圖所示:

2.CustomScroll類

CustomScroll:自定義滑動(dòng),該類包含了一個(gè)顯示滑動(dòng)條.邏輯如下所示:

  • 當(dāng)用戶只是單擊item時(shí),則不顯示.
  • 如果用戶點(diǎn)擊item進(jìn)行滑動(dòng)時(shí),則顯示.
  • 如果用戶滑動(dòng)后釋放鼠標(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,                   //按下移動(dòng)
        MOUSE_RELEASE_MOVE = 3                   //鼠標(biāo)離開并滑動(dòng)
    }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);            //將滑動(dòng)條移至最右側(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) //停下來(lái)了
    {
        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)的動(dòng)作——按下、放開、拖動(dòng),執(zhí)行相應(yīng)的操作
    if(mouse)
    {
        if( mouse->type() ==QEvent::MouseButtonPress)    //首次按下
        {
            pressMSec = QDateTime::currentDateTime().toMSecsSinceEpoch();     //記錄按下的時(shí)間
            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)   //未移動(dòng)
        {
            m_dragFlag = MOUSE_RELEASE;
            if(!m_scrollTimer.isActive())
                m_scrollTimer.start(1000);  //1S后取消滑動(dòng)條顯示
            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;       //計(jì)算每秒像素點(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后取消滑動(dòng)條顯示
                return true;
            }
            else
            {
 
                int moveValue ;
 
                 if(ms > 1000)      //滑動(dòng)的時(shí)間太長(zhǎng)
                {
                     m_dragFlag = MOUSE_RELEASE;
                     if(!m_scrollTimer.isActive())
                         m_scrollTimer.start(1000);  //1S后取消滑動(dòng)條顯示
                     return true;
                }
 
                if(releasePoint_y - pressPoint_y > 0)     //向下滑動(dòng)
                {
                    moveValue = m_scrollBar->value() - Pixel_per_second*0.2*(300/ms);//滑動(dòng)時(shí)間越長(zhǎng),moveValue值越小,因?yàn)椴皇强焖倩瑒?dòng)
                    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);  //定時(shí)刷新滑動(dòng)條顯示
 
                animation->start();
                m_dragFlag = MOUSE_RELEASE_MOVE;
 
            }
             return true;
        }
        else if(mouse->type() == QEvent::MouseMove && (m_dragFlag!= MOUSE_RELEASE) )
        {
            if(  m_dragFlag == MOUSE_PRESS)     //開始移動(dòng)
            {
                if(qAbs(dragPoint_y - mouse->pos().y()) < 4)      //判斷移動(dòng)閥值,避免誤操作
                    return true;
                else
                {
                   m_dragFlag = MOUSE_PRESS_MOVE;
                   if(m_selectTimer.isActive())                         //已經(jīng)移動(dò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);
}

以上就是Qt利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)的詳細(xì)內(nèi)容,更多關(guān)于Qt tablewidget滑動(dòng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語(yǔ)言實(shí)現(xiàn)順序表的順序查找和折半查找

    C語(yǔ)言實(shí)現(xiàn)順序表的順序查找和折半查找

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)順序表的順序查找和折半查找,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • C語(yǔ)言基礎(chǔ) strlen 函數(shù)

    C語(yǔ)言基礎(chǔ) strlen 函數(shù)

    這篇文章主要介紹了C語(yǔ)言基礎(chǔ) strlen 函數(shù),在C 語(yǔ)言中,char 字符串也是一種非常重要的數(shù)據(jù)類型,我們可以使用 strlen 函數(shù)獲取字符串長(zhǎng)度,這就是C語(yǔ)言strlen 函數(shù)的作用,下面我們來(lái)簡(jiǎn)單介紹該內(nèi)容,需要的朋友可以參考以下
    2021-10-10
  • C語(yǔ)言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法實(shí)例教程

    C語(yǔ)言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法實(shí)例教程

    這篇文章主要介紹了C語(yǔ)言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法,是C語(yǔ)言中非常重要的一個(gè)技巧,需要的朋友可以參考下
    2014-08-08
  • Qt QImage和QPixmap使用與區(qū)別

    Qt QImage和QPixmap使用與區(qū)別

    Qt中QImage類封裝了對(duì)于一般圖像像素級(jí)的操作,圖像顯示則使用QPixmap,本文主要介紹了Qt QImage和QPixmap使用與區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • 在C語(yǔ)言中對(duì)utmp文件進(jìn)行查找和寫入操作的函數(shù)小結(jié)

    在C語(yǔ)言中對(duì)utmp文件進(jìn)行查找和寫入操作的函數(shù)小結(jié)

    這篇文章主要介紹了在C語(yǔ)言中對(duì)utmp文件進(jìn)行查找和寫入操作的函數(shù)小結(jié),包括pututline()函數(shù)和getutline()函數(shù)以及getutid()函數(shù),需要的朋友可以參考下
    2015-08-08
  • C++命令行解析包gflags的使用教程

    C++命令行解析包gflags的使用教程

    這篇文章主要給大家介紹了關(guān)于C++命令行解析包gflags的使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 關(guān)于C/C++中可變參數(shù)的詳細(xì)介紹(va_list,va_start,va_arg,va_end)

    關(guān)于C/C++中可變參數(shù)的詳細(xì)介紹(va_list,va_start,va_arg,va_end)

    可變參數(shù)的函數(shù)原理其實(shí)很簡(jiǎn)單,而va系列是以宏定義來(lái)定義的,實(shí)現(xiàn)跟堆棧相關(guān).我們寫一個(gè)可變函數(shù)的C函數(shù)時(shí),有利也有弊,所以在不必要的場(chǎng)合,我們無(wú)需用到可變參數(shù)。如果在C++里,我們應(yīng)該利用C++的多態(tài)性來(lái)實(shí)現(xiàn)可變參數(shù)的功能,盡量避免用C語(yǔ)言的方式來(lái)實(shí)現(xiàn)
    2013-10-10
  • C語(yǔ)言編寫學(xué)生成績(jī)管理系統(tǒng)

    C語(yǔ)言編寫學(xué)生成績(jī)管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言編寫學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • c++中#include &lt;&gt;與#include""的區(qū)別詳細(xì)解析

    c++中#include &lt;&gt;與#include""的區(qū)別詳細(xì)解析

    <>先去系統(tǒng)目錄中找頭文件,如果沒(méi)有在到當(dāng)前目錄下找。所以像標(biāo)準(zhǔn)的頭文件 stdio.h、stdlib.h等用這個(gè)方法
    2013-10-10
  • c++ 讓程序開機(jī)自動(dòng)啟動(dòng)的方法

    c++ 讓程序開機(jī)自動(dòng)啟動(dòng)的方法

    這篇文章主要介紹了c++ 讓程序開機(jī)自動(dòng)啟動(dòng)的方法,需要的朋友可以參考下
    2017-09-09

最新評(píng)論

宝应县| 常山县| 康马县| 黔江区| 石楼县| 景东| 康保县| 乌什县| 普洱| 方正县| 文昌市| 金门县| 广河县| 皮山县| 新河县| 鄂托克前旗| 神池县| 怀化市| 耿马| 溧阳市| 山西省| 塔河县| 祁门县| 启东市| 景谷| 武义县| 铜山县| 开封县| 晋中市| 措美县| 太保市| 榆树市| 华容县| 尼勒克县| 宜阳县| 西丰县| 涟源市| 连州市| 汉中市| 平度市| 富裕县|