qt drawrect绘制图形为什么不显示_Qt编写自定义控件45-柱状标尺控件

本文介绍了如何使用Qt的drawRect方法创建一个自定义的柱状标尺控件,包括设置精度、颜色、线条步长和动画效果等功能。控件具有高度定制性和良好的跨平台支持,适用于各种Qt项目。
摘要由CSDN通过智能技术生成

一、前言

这个控件写了很久了,是最早期的一批控件中的一个,和温度计控件类似,都是垂直的进度条,可以设置不同的背景颜色,左侧的刻度也可以自由设定,还提供了动画效果,其实就是开启定时器慢慢的进度到设定的目标值,如果设定的值比当前值大,则递增,反之递减。由于当时的qpainter绘制功底还不够如火纯情,所以当时的刻度尺部分都是定死的字体大小,并不会随着控件变化而增大。

二、实现的功能

* 1:可设置精确度(小数点后几位)和间距

* 2:可设置背景色/柱状颜色/线条颜色

* 3:可设置长线条步长及短线条步长

* 4:可启用动画及设置动画步长

* 5:可设置范围值

* 6:支持负数刻度值

三、效果图

ae54db471f90b92b11aabe12a5559986.gif

四、核心代码

void RulerBar::paintEvent(QPaintEvent *){ //绘制准备工作,启用反锯齿 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //绘制背景 drawBg(&painter); //绘制标尺 drawRuler(&painter); //绘制柱状背景 drawBarBg(&painter); //绘制柱状 drawBar(&painter);}void RulerBar::drawBg(QPainter *painter){ painter->save(); painter->setPen(Qt::NoPen); QLinearGradient bgGradient(QPointF(0, 0), QPointF(0, height())); bgGradient.setColorAt(0.0, bgColorStart); bgGradient.setColorAt(1.0, bgColorEnd); painter->setBrush(bgGradient); painter->drawRect(rect()); painter->restore();}void RulerBar::drawRuler(QPainter *painter){ painter->save(); painter->setPen(lineColor); //绘制纵向标尺线 20的长度为刻度尺文字的宽度 double initX = space + 20; double initY = space; QPointF topPot(initX, initY); QPointF bottomPot(initX, height() - space); painter->drawLine(topPot, bottomPot); //绘制纵向标尺刻度 double length = height() - 2 * space; //计算每一格移动多少 double increment = length / (maxValue - minValue); //长线条短线条长度 int longLineLen = 10; int shortLineLen = 7; //根据范围值绘制刻度值及刻度值 for (int i = maxValue; i >= minValue; i = i - shortStep) { if (i % longStep == 0) { QPointF leftPot(initX + longLineLen, initY); QPointF rightPot(initX, initY); painter->drawLine(leftPot, rightPot); QString strValue = QString("%1").arg((double)i, 0, 'f', precision); double fontWidth = painter->fontMetrics().width(strValue); double fontHeight = painter->fontMetrics().height(); QPointF textPot(initX - fontWidth - 5, initY + fontHeight / 3); painter->drawText(textPot, strValue); } else { if (i % (longStep / 2) == 0) { shortLineLen = 7; } else { shortLineLen = 4; } QPointF leftPot(initX + shortLineLen, initY); QPointF rightPot(initX, initY); painter->drawLine(leftPot, rightPot); } initY += increment * shortStep; } painter->restore();}void RulerBar::drawBarBg(QPainter *painter){ painter->save(); painter->setPen(Qt::NoPen); //20的长度为刻度尺文字的宽度 15为刻度尺到柱状图的宽度 double initX = space + 20 + 15; QPointF topLeftPot(initX, space); QPointF bottomRightPot(width() - space , height() - space); barRect = QRectF(topLeftPot, bottomRightPot); painter->setBrush(barBgColor); painter->drawRect(barRect); painter->restore();}void RulerBar::drawBar(QPainter *painter){ painter->save(); painter->setPen(Qt::NoPen); double barHeight = barRect.height(); double increment = (double)barHeight / (maxValue - minValue); double initY = (currentValue - minValue) * increment; QPointF topLeftPot(barRect.topLeft().x(), barRect.bottomLeft().y() - initY); QPointF bottomRightPot(barRect.bottomRight()); QRectF currentRect(topLeftPot, bottomRightPot); painter->setBrush(barColor); painter->drawRect(currentRect); painter->restore();}

六、控件介绍

1. 超过150个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。

2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。

3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,支持任意操作系统比如windows+linux+mac+嵌入式linux等,不乱码,可直接集成到Qt Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。

4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。

5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。

6. 每个控件默认配色和demo对应的配色都非常精美。

7. 超过130个可见控件,6个不可见控件。

8. 部分控件提供多种样式风格选择,多种指示器样式选择。

9. 所有控件自适应窗体拉伸变化。

10. 集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。

11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。

12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。

13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。

14. 目前已经有qml版本,后期会考虑出pyqt版本,如果用户需求量很大的话。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值