Qt自繪實(shí)現(xiàn)蘋果按鈕滑動(dòng)效果的示例代碼
用到的類:QTimer,QPaintEvent,QPainter,QRectF
首先,重寫繪制事件,需要在頭文件加入QPaintEvent頭文件,并定義幾個(gè)變量。
bool ison=false; float currentValue; float widthSize,heightSize;
然后加入如下代碼:
思路就是鼠標(biāo)點(diǎn)擊,觸發(fā)paintEvent函數(shù)
void MainWindow::mousePressEvent(QMouseEvent *event){ Q_UNUSED(event) ison=!ison; //在頭文件種定義:bool ison=false; //當(dāng)鼠標(biāo)點(diǎn)擊,ison為true; timer->start(1);//定時(shí)器開始(ms級) this->update();//觸發(fā)paintEvent函數(shù) }
paintEvent函數(shù)的重寫
void MainWindow::paintEvent(QPaintEvent *event){ Q_UNUSED(event) QPainter painter(this); painter.setRenderHint(QPainter::SmoothPixmapTransform); //QPainter::SmoothPixmapTransform 使用平滑的pixmap變換算法(雙線性插值算法),而不是近鄰插值算。 painter.setRenderHint(QPainter::Antialiasing); //使繪制時(shí)邊緣平滑,qt反走樣默認(rèn)關(guān)閉 painter.setPen(Qt::NoPen);//畫筆樣式,這里無 if(ison){ painter.save();//保存當(dāng)前畫筆的狀態(tài),與下面的restore();成對出現(xiàn) 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();//恢復(fù)畫筆 //save() 用于保存 QPainter 的狀態(tài),restore() 用于恢復(fù) QPainter 的狀態(tài),save() 和 restore() 一般都是成對使用的, //如果只調(diào)用了 save() 而不調(diào)用 restore(),那么保存就沒有意義了,保存是為了能恢復(fù)被保存的狀態(tài)而使用的。 }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); //第1、2個(gè)參數(shù)制定矩形的左上角起點(diǎn),第3個(gè)參數(shù)制定矩形的長度,第4個(gè)參數(shù)指定矩形的寬度 //最后兩個(gè)參數(shù)決定角的圓度。它可以為0到99之間的任意值(99代表最圓)。 //繪制圓形矩形 painter.restore(); //按鈕 painter.save(); painter.setBrush(Qt::white); painter.drawEllipse(currentValue,0.05*heightSize,0.9*heightSize,0.9*heightSize); //第1,2個(gè)參數(shù)表示圓/橢圓距屏幕左上角的像素?cái)?shù)。第3,4個(gè)參數(shù)表示圓/橢圓的寬度和高度,兩者相同時(shí)為圓。 //繪制圓按鈕 painter.restore(); } }
鼠標(biāo)點(diǎn)擊進(jìn)行繪制,按鈕從左邊滑到右邊應(yīng)該有一個(gè)運(yùn)動(dòng)狀態(tài)。這就是定時(shí)器。
在窗體構(gòu)造函數(shù)中進(jìn)行信號綁定:
timer=new QTimer(this); timer->setInterval(50); connect(timer,SIGNAL(timeout()),this,SLOT(begainAnimation())); //下面是繪制參數(shù)相關(guān) if(ison){ currentValue=widthSize-0.95*heightSize; }else{ currentValue=0.05*heightSize; }
然后編寫begainAnimation函數(shù):
void MainWindow::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(); //每1ms調(diào)用一次updata。 }
繪制矩形:paint->drawRect(20,20,160,160);
第1、2個(gè)參數(shù)制定矩形的左上角起點(diǎn),第3個(gè)參數(shù)制定矩形的長度,第4個(gè)參數(shù)指定矩形的寬度
繪制圓和橢圓:paint->drawEllipse(20,20,210,160);
第1,2個(gè)參數(shù)表示圓/橢圓距屏幕左上角的像素?cái)?shù)。第3,4個(gè)參數(shù)表示圓/橢圓的寬度和高度,兩者相同時(shí)為圓。
繪制圓角矩形:paint->drawRoundRect(20,20,210,160,50,50);
前面四個(gè)參數(shù)和繪制矩形的參數(shù)一致,最后兩個(gè)參數(shù)決定角的圓度。它可以為0到99之間的任意值(99代表最圓)。
到此這篇關(guān)于Qt自繪實(shí)現(xiàn)蘋果按鈕滑動(dòng)效果的示例代碼的文章就介紹到這了,更多相關(guān)Qt 蘋果按鈕滑動(dòng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++學(xué)習(xí)之Lambda表達(dá)式的用法詳解
Lambda?表達(dá)式(lambda?expression)是一個(gè)匿名函數(shù),Lambda表達(dá)式基于數(shù)學(xué)中的λ演算得名。本文就來為大家詳細(xì)講講C++中Lambda表達(dá)式的使用,需要的可以參考一下2022-07-07C語言實(shí)現(xiàn)可保存的動(dòng)態(tài)通訊錄的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)一個(gè)簡單的可保存的動(dòng)態(tài)通訊錄,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下2022-07-07C++實(shí)現(xiàn)四則運(yùn)算器(帶括號)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)四則運(yùn)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11C/C++哈希表優(yōu)化LeetCode題解997找到小鎮(zhèn)的法官
這篇文章主要為大家介紹了C/C++哈希表優(yōu)化題解997找到小鎮(zhèn)的法官示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12