您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何使用Qt自定義控件實現簡易儀表盤,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Qt自定義控件12:簡易儀表盤
思路:畫270度的圓弧,圓弧根據占比分為兩種顏色,根據占比在圓弧上畫出一個圓球作為標志,然后就是刻度線和刻度值。刻度線是根據坐標系旋轉畫出,刻度值是根據角度求出x,y坐標值構造出一個矩形畫出刻度值(不要用坐標系旋轉畫刻度值,那樣刻度值的角度也會旋轉,寫出的字不是正的,效果不好)。最后就是在中心畫value。
關鍵代碼:
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();}
關于“如何使用Qt自定義控件實現簡易儀表盤”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。