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