Qt利用QScroller實(shí)現(xiàn)home界面滑動效果
在學(xué)習(xí)本章之前需要知道滑動的關(guān)鍵詞:
鼠標(biāo)按下,鼠標(biāo)滑動 : 指的是用戶按下屏幕,然后進(jìn)行移動的操作,此時用戶滑動多少距離,那么視圖就偏移多少距離.
平滑滑動 : 指的是手指離開屏幕了,然后會讀取滑動的速率(距離/時間),從而讓視圖自己平滑的再滑動一段距離.
1.QScroller類
用于觸摸屏的一個滑動器,實(shí)現(xiàn)用戶用手指來滑動視圖,有大量的參數(shù)設(shè)置可以通過QScrollerProperties設(shè)置,它的默認(rèn)值基于平臺優(yōu)化值。
2.QScrollerProperties滑動器參數(shù)類
QScrollerProperties類存儲QScroller使用到的參數(shù),默認(rèn)設(shè)置與平臺相關(guān),以便Qt模擬平臺行為進(jìn)行動態(tài)滾動。當(dāng)然也可以由用戶設(shè)置參數(shù)。
可以設(shè)置的參數(shù)類型有以下幾種:
QScrollerProperties::MousePressEventDelay //設(shè)置鼠標(biāo)按下的延遲時間,比如設(shè)置按下多少ms后開始真正觸發(fā)滑動器. QScrollerProperties::DragStartDistance //設(shè)置移動閥值(按下后需要移動最少多少距離后,觸發(fā)滑動),用來避免誤操作. QScrollerProperties::DragVelocitySmoothingFactor //平滑滑動速度,當(dāng)滑動后手離開屏幕后,進(jìn)行平滑滑動的速度,此值應(yīng)介于0和1之間。值越小,速度越慢。 QScrollerProperties::DecelerationFactor //減速速度因子,減速到0速度所需的時間。對于大多數(shù)類型,該值應(yīng)在0.1到2.0的范圍內(nèi) QScrollerProperties::MinimumVelocity //平滑滑動的最小速度 QScrollerProperties::MaximumVelocity //平滑滑動的最大速度 QScrollerProperties::SnapTime //滾動曲線的時間因子。設(shè)置滾動的時長,值越小,滾動時間越長. QScrollerProperties::FrameRate //設(shè)置平滑滑動的刷新頻率,可以設(shè)置的值有如下幾種: //QScrollerProperties::Fps60 //QScrollerProperties::Fps30 //QScrollerProperties::Fps20 QScrollerProperties::VerticalOvershootPolicy //垂直越區(qū)策略
3.表格類的使用示例
代碼如下所示:
table = new QTableWidget(this); table->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); QScroller* scroller = QScroller::scroller(table->viewport()); //也可以填入直接填入table,只是點(diǎn)擊滑動條時會無反應(yīng). scroller->grabGesture(table->viewport(),QScroller::LeftMouseButtonGesture); QScrollerProperties properties = scroller->scrollerProperties(); properties.setScrollMetric(QScrollerProperties::DragVelocitySmoothingFactor,0.3); properties.setScrollMetric(QScrollerProperties::FrameRate,QScrollerProperties::Fps60); scroller->setScrollerProperties(properties);
4.自定義home界面Demo示例
界面如下所示

當(dāng)然可以自定義icon列數(shù),寬高,icon之間的間距,比如橫屏:

滑動效果圖如下所示:

支持界面自定義拖動,效果圖如下所示:

由于我們要使用QScroller,所以需要子類化QTableWidget.ScrollHome.h如下所示:
class ScrollHome : public QTableWidget
{
Q_OBJECT
int iconSize;
QSize itemSize;
int iconSpacing;
int iconColumn;
QMargins margins;
QFont font;
QPalette palette;
QWidget *parent;
QPushButton* newAppButton(QWidget* app,AppRes& appRes);
QWidget* newAppIcon(AppRes& appRes);
public:
explicit ScrollHome(QWidget *parent );
void InitHome(int iconColumn, int iconSpacing, int widgetWidth, int widgetHeight );
int GetIconSize();
signals:
void appClicked(AppRes* app);
protected slots:
void onIcon();
};其中InitHome()用來初始化整個home用的.newAppIcon()用來初始化每個app圖標(biāo)用的.函數(shù)如下所示:
QWidget* ScrollHome::newAppIcon(AppRes& appRes)
{
QWidget *app = new QWidget(this);
app->setFixedSize(itemSize);
QPushButton *btn = newAppButton(app,appRes);
btn->move(iconSpacing/2,iconSpacing/2);
QLabel *label = new QLabel(appRes.appName,app);
label->setFont(font);
label->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
label->setGeometry(0,iconSize + btn->y()+4,itemSize.width(),itemSize.height()-iconSize - btn->y());
label->setPalette(palette);
return app;
}
void ScrollHome::InitHome(int iconColumn, int iconSpacing, int widgetWidth, int widgetHeight)
{
this->iconColumn = iconColumn;
this->iconSpacing = iconSpacing;
this->setFixedSize(widgetWidth, widgetHeight);
this->viewport()->setFixedWidth(widgetWidth);
iconSize = (widgetWidth - iconSpacing * iconColumn)/iconColumn;
font.setPixelSize(iconSize/5.2);
itemSize = QSize(iconSize + iconSpacing,iconSize+iconSpacing + font.pixelSize() +10);
this->setColumnCount(iconColumn);
for(int i = 0; i < iconColumn; ++i)
{
this->setColumnWidth(i,itemSize.width());
}
this->clearContents();
int rowCount = g_appResSize/iconColumn +(g_appResSize%iconColumn==0 ? 0 : 1);
this->setRowCount(rowCount);
for(int i = 0; i < rowCount; ++i) {
for(int j = 0; j < iconColumn; ++j) {
int crtIndex = i * iconColumn + j;
if(crtIndex >= g_appResSize)
break;
g_appRes[crtIndex].resIndex = crtIndex;
this->setCellWidget(i, j, newAppIcon(g_appRes[crtIndex]));
this->setRowHeight(i,itemSize.height());
}
}
}初始化完成后,即可通過QScroller進(jìn)行綁定滑動器.
到此這篇關(guān)于Qt利用QScroller實(shí)現(xiàn)home界面滑動效果的文章就介紹到這了,更多相關(guān)Qt QScroller界面滑動效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)假裝藍(lán)屏整蠱小程序
因?yàn)楣ぷ鞯男枰袝r候我們離開時并不想讓別人看到我們電腦的內(nèi)容,所以本文為大家分享了一個基于C++實(shí)現(xiàn)的假裝藍(lán)屏的小程序,需要的可以參考下2023-06-06
C++實(shí)現(xiàn)LeetCode(128.求最長連續(xù)序列)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(128.求最長連續(xù)序列),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Qt實(shí)現(xiàn)SqlRelationalTable關(guān)聯(lián)表組件
在Qt中我們可以通過拖拽的方式將不同組件放到指定的位置,實(shí)現(xiàn)圖形化開發(fā)極大的方便了開發(fā)效率,本章將重點(diǎn)介紹SqlRelationalTable關(guān)聯(lián)表組件的常用方法及靈活運(yùn)用,感興趣的可以了解一下2023-12-12
OpenCV實(shí)現(xiàn)區(qū)域分割和區(qū)域生長
區(qū)域分割是圖像處理中一個重要的任務(wù),本文主要介紹了OpenCV實(shí)現(xiàn)區(qū)域分割和區(qū)域生長,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02

