Qt自定義控件實(shí)現(xiàn)簡(jiǎn)易儀表盤
本文實(shí)例為大家分享了Qt自定義控件實(shí)現(xiàn)簡(jiǎn)易儀表盤的具體代碼,供大家參考,具體內(nèi)容如下
Qt自定義控件12:簡(jiǎn)易儀表盤(根據(jù)liudianwu大神的界面自己寫的代碼,建議去學(xué)習(xí)劉大神,會(huì)受益良多的)
先看效果圖:

思路:畫270度的圓弧,圓弧根據(jù)占比分為兩種顏色,根據(jù)占比在圓弧上畫出一個(gè)圓球作為標(biāo)志,然后就是刻度線和刻度值??潭染€是根據(jù)坐標(biāo)系旋轉(zhuǎn)畫出,刻度值是根據(jù)角度求出x,y坐標(biāo)值構(gòu)造出一個(gè)矩形畫出刻度值(不要用坐標(biāo)系旋轉(zhuǎn)畫刻度值,那樣刻度值的角度也會(huì)旋轉(zhuǎn),寫出的字不是正的,效果不好)。最后就是在中心畫value。
關(guān)鍵代碼:
void CMPassrate5::paintEvent(QPaintEvent *event){
int width = this->width();
int height = this->height();
int side = qMin(width, height);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.translate(width / 2, height / 2);
painter.scale(side / 200.0, side / 200.0);
drawE(&painter);
drawEPoint(&painter);
drawLine(&painter);
drawEText(&painter);
drawValue(&painter);
}
void CMPassrate5::drawE(QPainter* painter){
QRect rect(-radius,-radius,2*radius,2*radius);
painter->save();
painter->setPen(Qt::NoPen);
QPainterPath path;
QPainterPath subPath;
QPainterPath outPath;
QPainterPath outPubPath;
outPath.arcTo(rect,-45,outRange);
outPubPath.addEllipse(rect.adjusted(side,side,-side,-side));
outPath -= outPubPath;
color.setAlpha(100);
painter->setBrush(color);
painter->drawPath(outPath);
path.arcTo(rect,-45+outRange,range);
subPath.addEllipse(rect.adjusted(4,4,-4,-4));
path -= subPath;
color.setAlpha(180);
painter->setBrush(color);
painter->drawPath(path);
painter->restore();
}
void CMPassrate5::drawEPoint(QPainter* painter){
//圓球位置就在outRange盡頭處
painter->save();
color.setAlpha(180);
painter->setPen(Qt::NoPen);
painter->setBrush(color);
float x = (radius-side/2)*qCos((range+135)*3.14/180);
float y = (radius-side/2)*qSin((range+135)*3.14/180);
qDebug()<<"x:"<<x<<" y:"<<y;
painter->drawEllipse(QPoint(x,y),side,side);
painter->restore();
}
void CMPassrate5::drawLine(QPainter* painter){
painter->save();
painter->rotate(135);
color.setAlpha(100);
painter->setPen(color);
QLine line(QPoint(radius-side-lineLength,0),QPoint(radius-side,0));
for(int i = 0;i<lineCount;i++){
painter->drawLine(line);
painter->rotate(270.0/lineCount);
}
painter->restore();
}
void CMPassrate5::drawEText(QPainter* painter){
painter->save();
// painter->rotate(135);
painter->setPen(Qt::black);
float textRange = 270.0/(textCount-1);
float x,y;
for(int i = 0;i<=10;i++){
x = (radius-side-lineLength)*qCos((textRange*i+135)*3.14/180);
y = (radius-side-lineLength)*qSin((textRange*i+135)*3.14/180);
if(i<5){
QRect rect(x,y-4,20,10);
painter->drawText(rect,Qt::AlignCenter,QString::number(i*10));
}else if(i ==5){
QRect rect(x-7,y,20,10);
painter->drawText(rect,Qt::AlignCenter,QString::number(i*10));
}else{
QRect rect(x-20,y-5,20,10);
painter->drawText(rect,Qt::AlignCenter,QString::number(i*10));
}
}
painter->restore();
}
void CMPassrate5::drawValue(QPainter* painter){
painter->save();
QPen pen = painter->pen();
pen.setColor(color);
pen.setWidth(2);
painter->setPen(pen);
QFont font = painter->font();
font.setPixelSize(45);
painter->setFont(font);
QRect rect(-25,-25,50,50);
painter->drawText(rect,Qt::AlignCenter,QString::number(value));
painter->restore();
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
下一篇:C語言實(shí)現(xiàn)倉庫物資管理系統(tǒng)
本文標(biāo)題:Qt自定義控件實(shí)現(xiàn)簡(jiǎn)易儀表盤
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/29.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10用C語言實(shí)現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實(shí)現(xiàn)單鏈表的各種操作(二)


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)


