Qt模仿IOS滑動(dòng)按鈕效果
在上一篇文章里我介紹了在Android中如何實(shí)現(xiàn)IOS形式的滑動(dòng)按鈕,在這篇文章中我將介紹如何用Qt實(shí)現(xiàn)IOS形式的滑動(dòng)按鈕。其實(shí)在Android中實(shí)現(xiàn)這個(gè)和在Qt中實(shí)現(xiàn)是一樣的道理的,只是使用的工具有所不同罷了。在Qt里面我們使用的是C++,而Android中則是Java。語(yǔ)言并不是決定的因素,而實(shí)現(xiàn)的思路才是最終決定勝負(fù)的利器。
1)、在Android中的繪制主要是在OnDraw這個(gè)函數(shù)里面進(jìn)行的,且可以在OnDraw外部寫函數(shù)進(jìn)行繪制,只需把Cavas傳入即可。而在Qt里面的繪制主要是在painEvent里面進(jìn)行的,且不能再外部寫函數(shù)實(shí)現(xiàn)它的繪制。
2)、在Android中承擔(dān)繪制的主要是Canvas這個(gè)對(duì)象,Painter主要是來(lái)進(jìn)行畫筆的定義和修改。而在Qt里面主要承擔(dān)繪制任務(wù)的是Painter對(duì)象,它既要充當(dāng)畫筆的角色,還要做為畫板來(lái)存在。
3)、在Android里面我們可以使用ValueAnimation來(lái)實(shí)現(xiàn)動(dòng)畫刷新,而在Qt里面并沒用提供這樣的一個(gè)函數(shù),所以我們只能通過QTimer來(lái)主動(dòng)刷新,具體代碼在下方。
4)、在兩份代碼里面懂提供了外部接口來(lái)訪問和讀寫它的狀態(tài)。
代碼如下
1、switchButton的頭文件
#ifndef SWITCHBUTTON_H #define SWITCHBUTTON_H #include <QWidget> #include<QTimer> class switchButton : public QWidget { Q_OBJECT public: explicit switchButton(QWidget *parent = 0); void writeSwitchButtonState(bool ison); bool readSwitchButtonState(); private: bool ison=false; float currentValue; float widthSize,heightSize; QTimer *timer; void paintEvent(QPaintEvent *event);//繪制事件 void mousePressEvent(QMouseEvent *event);//點(diǎn)擊事件 signals: public slots: private slots: void begainAnimation(); }; #endif // SWITCHBUTTON_H
2、switchButton的源文件
#include "switchbutton.h" #include <QPaintEvent> #include<QPainter> #include<QRectF> #include<QRect> /** * @brief switchButton::switchButton * @param parent * 創(chuàng)建的這個(gè)switchbutton只是提供固定的大小,展示實(shí)現(xiàn)的過程。 */ switchButton::switchButton(QWidget *parent) : QWidget(parent) { setMaximumSize(200,130); setMinimumSize(200,130); widthSize=(float)width(); heightSize=(float)height(); timer=new QTimer(this); timer->setInterval(50); if(ison){ currentValue=widthSize-0.95*heightSize; }else{ currentValue=0.05*heightSize; } connect(timer,SIGNAL(timeout()),this,SLOT(begainAnimation())); } void switchButton::paintEvent(QPaintEvent *event){ Q_UNUSED(event) QPainter painter(this); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(Qt::NoPen); if(ison){ painter.save(); painter.setBrush(Qt::green); QRectF greenRect=QRectF(0,0,widthSize,heightSize); painter.drawRoundedRect(greenRect,0.5*heightSize,0.5*heightSize); painter.restore(); painter.save(); painter.setBrush(Qt::white); painter.drawEllipse(currentValue,0.05*heightSize,0.9*heightSize,0.9*heightSize); painter.restore(); }else{ painter.save(); QColor grayColor(199,199,199); painter.setBrush(grayColor); QRectF roundRect=QRectF(0,0,widthSize,heightSize); painter.drawRoundedRect(roundRect,0.5*heightSize,0.5*heightSize); painter.restore(); painter.save(); painter.setBrush(Qt::red); QRectF redRect=QRectF(heightSize*0.05,heightSize*0.05,widthSize-heightSize*0.1,heightSize*0.9); painter.drawRoundedRect(redRect,0.45*heightSize,0.45*heightSize); painter.restore(); painter.save(); painter.setBrush(Qt::white); painter.drawEllipse(currentValue,0.05*heightSize,0.9*heightSize,0.9*heightSize); painter.restore(); } } void switchButton::mousePressEvent(QMouseEvent *event){ Q_UNUSED(event) ison=!ison; timer->start(10); this->update(); } void switchButton::begainAnimation(){ int i=0.05*heightSize; int n=widthSize-0.95*heightSize; if(ison){ currentValue+=1; if(currentValue>n-i){ timer->stop(); } }else{ currentValue-=1; if(currentValue<i){ timer->stop(); } } update(); } void switchButton::writeSwitchButtonState(bool ison) { this->ison=ison; this->update(); } bool switchButton::readSwitchButtonState() { return this->ison; }
鑒于QTimer的復(fù)雜,本例里面沒有對(duì)透明度進(jìn)行動(dòng)畫過渡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Matlab實(shí)現(xiàn)獲取文件夾下所有指定后綴的文件
這篇文章主要為大家詳細(xì)介紹了Matlab如何獲取文件夾下所有指定后綴的文件(包含子文件夾),文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下2022-11-11Qt一個(gè)進(jìn)程運(yùn)行另一個(gè)進(jìn)程的實(shí)現(xiàn)方法
本文主要介紹了Qt一個(gè)進(jìn)程運(yùn)行另一個(gè)進(jìn)程的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04C語(yǔ)言實(shí)現(xiàn)高精度加法的示例代碼
高精度的本質(zhì)是將數(shù)字以字符串的形式讀入,然后將每一位分別存放入int數(shù)組中,通過模擬每一位的運(yùn)算過程,來(lái)實(shí)現(xiàn)最終的運(yùn)算效果,下面我們就來(lái)看看如何通過C語(yǔ)言實(shí)現(xiàn)高精度加法吧2023-11-11關(guān)于讀取popen輸出結(jié)果時(shí)未截?cái)嘧址畬?dǎo)致的命令行注入詳解
這篇文章主要給大家介紹了關(guān)于讀取popen輸出結(jié)果時(shí)未截?cái)嘧址畬?dǎo)致的命令行注入的相關(guān)資料,文中通過圖文及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03用C語(yǔ)言求冪函數(shù)和指數(shù)函數(shù)的方法
這篇文章主要介紹了用C語(yǔ)言求冪函數(shù)和指數(shù)函數(shù)的方法,即pow()函數(shù)和sqrt()函數(shù)的使用,需要的朋友可以參考下2015-08-08OpenCV實(shí)現(xiàn)圖像角點(diǎn)檢測(cè)
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像角點(diǎn)檢測(cè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01