Qt自定義控件實現(xiàn)簡易儀表盤
本文實例為大家分享了Qt自定義控件實現(xiàn)簡易儀表盤的具體代碼,供大家參考,具體內容如下
Qt自定義控件12:簡易儀表盤(根據(jù)liudianwu大神的界面自己寫的代碼,建議去學習劉大神,會受益良多的)
先看效果圖:
思路:畫270度的圓弧,圓弧根據(jù)占比分為兩種顏色,根據(jù)占比在圓弧上畫出一個圓球作為標志,然后就是刻度線和刻度值??潭染€是根據(jù)坐標系旋轉畫出,刻度值是根據(jù)角度求出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(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(二)


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結