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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(73.矩陣賦零)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(73.矩陣賦零),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++實(shí)現(xiàn)LeetCode(347.前K個(gè)高頻元素)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(347.前K個(gè)高頻元素),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C++ clock()解析如何使用時(shí)鐘計(jì)時(shí)的應(yīng)用
本篇文章是對(duì)c++中的clock()函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C語言 結(jié)構(gòu)體數(shù)組詳解及示例代碼
本文主要介紹C語言 結(jié)構(gòu)體數(shù)組,這里整理了相關(guān)資料及簡(jiǎn)單示例代碼,以便大家學(xué)習(xí)參考,有興趣的小伙伴可以看下2016-08-08