QT 使用QPainter 绘制图形 和 世界变换 world transform
1. 绘制椭圆 饼状型 贝塞尔曲线
绘制图像重写方法 void paintEvent(QPaintEvent *event)即可。
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter1(this); //新建画笔
painter1.setRenderHint(QPainter::Antialiasing, true); //设置 反走样
painter1.setPen(QPen(Qt::black, 8, Qt::DashDotLine, Qt::RoundCap)); //设置线条 颜色, 大小,线型风格,拐点风格
painter1.setBrush(QBrush(Qt::green, Qt::SolidPattern)); //设置画刷 颜色 和 风格
painter1.drawEllipse(10, 10, 160, 80); //画椭圆 QPainter painter2(this);
painter2.setRenderHint(QPainter::Antialiasing, true);
painter2.setPen(QPen(Qt::black, 8, Qt::DashDotLine, Qt::RoundCap));
painter2.setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
painter2.drawPie(10, 150, 200, 120, 60*16, 270*16); QPainter painter3(this);
painter3.setRenderHint(QPainter::Antialiasing, true);
QPainterPath path; //轨迹
path.moveTo(200, 150); //先移到这个点
path.cubicTo(250, 80, 320, 80, 400, 150); //x1 x2 和 andpoint 确定弧形
painter3.setPen(QPen(Qt::black, 8));
painter3.setBrush(QBrush(Qt::green, Qt::DiagCrossPattern)); QTransform transform; //世界变换
transform.translate(0, 0); //以00为中心 选装30°
transform.rotate(+30.0);
painter3.setWorldTransform(transform); //添加世界变换
painter3.drawPath(path);
}
2. 仪表盘
oventimer.h
#ifndef OVENTIMER_H
#define OVENTIMER_H #include <QWidget>
#include <QtGui> class OvenTimer : public QWidget
{
Q_OBJECT public:
OvenTimer(QWidget *parent = 0); void setDuration(int secs);
int duration() const;
void draw(QPainter *painter); signals:
void timeout(); protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event); private:
QDateTime finishTime;
QTimer *updateTimer;
QTimer *finishTimer;
}; #endif // OVENTIMER_H
oventimer.cpp
#include "oventimer.h"
#include <QtGui>
#include <QtCore>
#include <math.h> const double DegreesPerMinute = 7.0;
const double DegressPerSecond = DegreesPerMinute/60;
const int MaxMinutes = 45;
const int MaxSeconds = MaxMinutes*60;
const int UpdateInterval = 5; OvenTimer::OvenTimer(QWidget *parent)
: QWidget(parent)
{
finishTime = QDateTime::currentDateTime();
updateTimer = new QTimer(this);
connect(updateTimer, SIGNAL(timeout()), this, SLOT(update())); finishTimer = new QTimer(this);
finishTimer->setSingleShot(true);
connect(finishTimer, SIGNAL(timeout()), this, SIGNAL(stop()));
connect(finishTimer, SIGNAL(timeout()), updateTimer, SLOT(stop())); QFont font;
font.setPointSize(8);
setFont(font);
} void OvenTimer::setDuration(int secs)
{
secs = qBound(0, secs, MaxSeconds);
finishTime = QDateTime::currentDateTime().addSecs(secs);
if( secs > 0 )
{
updateTimer->start(UpdateInterval*1000);
finishTimer->start(secs*1000);
}
else
{
updateTimer->stop();
finishTimer->stop();
}
update();
} int OvenTimer::duration() const
{
int secs = QDateTime::currentDateTime().secsTo(finishTime);
if( secs < 0 )
secs = 0;
return secs;
} void OvenTimer::mousePressEvent(QMouseEvent *event)
{
QPointF point = event->pos() - rect().center();
double theta = atan2(-point.x(), -point.y())*180.0/M_PI;
setDuration(duration() + int(theta / DegressPerSecond));
update();
} void OvenTimer::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
int side = qMin(width(), height());
painter.setViewport( (width()-side)/2, (height()-side)/2, side, side );
painter.setWindow(-50, -50, 100, 100);
draw(&painter);
} void OvenTimer::draw(QPainter *painter)
{
static const int triangle[3][2]={ {-2,-49}, {+2,-49}, {0,-47} };
QPen thickPen(palette().foreground(), 1.5);
QPen thinPen(palette().foreground(), 0.5);
QColor niceBlue(150, 150, 200); painter->setPen(thinPen);
painter->setBrush(palette().foreground());
painter->drawPolygon( QPolygon(3, &triangle[0][0]) ); QConicalGradient coneGradient(0,0,-90.0);
coneGradient.setColorAt(0.0, Qt::darkGray);
coneGradient.setColorAt(0.2, niceBlue);
coneGradient.setColorAt(0.5, Qt::white);
coneGradient.setColorAt(1.0, Qt::darkGray);
painter->setBrush(coneGradient);
painter->drawEllipse(-46, -46, 92, 92); QRadialGradient haloGradient(0,0,20,0,0);
haloGradient.setColorAt(0.0, Qt::lightGray);
haloGradient.setColorAt(0.8, Qt::darkGray);
haloGradient.setColorAt(0.9, Qt::white);
haloGradient.setColorAt(1.0, Qt::black);
painter->setPen(Qt::NoPen);
painter->setBrush(haloGradient);
painter->drawEllipse(-20, -20, 40, 40); QLinearGradient knobGradient(-7, -25, 7, -25);
knobGradient.setColorAt(0.0, Qt::black);
knobGradient.setColorAt(0.2, niceBlue);
knobGradient.setColorAt(0.3, Qt::lightGray);
knobGradient.setColorAt(0.8, Qt::white);
knobGradient.setColorAt(1.0, Qt::black); painter->rotate(duration() * DegressPerSecond);
painter->setBrush(knobGradient);
painter->setPen(thinPen);
painter->drawRoundedRect(-7, -25, 14, 50, 99, 49); for( int i=0; i<=MaxMinutes; i++ )
{
if(i%5==0)
{
painter->setPen(thickPen);
painter->drawLine(0, -41, 0, -44);
painter->drawText(-15, -41, 30, 30,
Qt::AlignHCenter | Qt::AlignTop,QString::number(i));
}
else
{
painter->setPen(thinPen);
painter->drawLine(0, -42, 0, -44);
}
painter->rotate(-DegreesPerMinute);
} }
QT 使用QPainter 绘制图形 和 世界变换 world transform的更多相关文章
- Qt使用QPainter绘制矢量图并保存为svg文件
位图和矢量图: Bitmap: Usually a larger file size Cannot be enlarged into a higher resolution as the image ...
- PyQt5利用QPainter绘制各种图形
这个例子我做了好几天: 1)官网C++的源码,改写成PyQt5版本的代码,好多细节不会转化 2)网上的PyQt的例子根本运行不了 填了无数个坑,结合二者,终于能完成了一个关于绘图的东西.这个过程也掌握 ...
- 利用QPainter绘制各种图形(Shape, Pen 宽带,颜色,风格,Cap,Join,刷子)
利用QPainter绘制各种图形 Qt的二维图形引擎是基于QPainter类的.QPainter既可以绘制几何形状(点.线.矩形.椭圆.弧形.弦形.饼状图.多边形和贝塞尔曲线),也可以绘制像素映射.图 ...
- Qt开发技术:图形视图框架(二)场景QGraphicsScene、QGraphicsItem与QGraphicsView详解
前话 Qt的图形视图框架,最核心的三个类为:QGraphicsScene.QGraphicsItem与QGraphicsView. 基于图形框架的高级白板软件Demo QGraphicsSce ...
- QPainter绘制遇到的小问题
1.Qt绘图基础 (1)绘图需画笔和画布: QPainter 相当于Qt中的一个画笔,绘制时需要一块画布, Qt中扮演画布角色的组件为QPaintDevice和他的各个子类,如: QWidget, Q ...
- qt中窗口绘制——图片的绘制
在qt 中,QPixmap 用于表示一张图片,支持png,jpg格式的加载. QPixmap pm("c:/test.png"); 或者 QPixmap pm; pm.load(& ...
- Java 在PDF文档中绘制图形
本篇文档将介绍通过Java编程在PDF文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...
- OpenGL学习进程(4)第二课:绘制图形
本节是OpenGL学习的第二个课时,下面介绍如何用点和线来绘制图形: (1)用点的坐标来绘制矩形: #include <GL/glut.h> void display(void) ...
- 46.Qt 使用OpenGL绘制立方体
main.cpp #include <QApplication> #include <iostream> #include "vowelcube.h" in ...
随机推荐
- 学习 SSH
ssh_config 与 sshd_config ssh_config: configuration file for the ssh client on the host machine you a ...
- Objective-c 单例模式
用GCD写Objective-c的单例模式和C#有比较大的区别 声明h文件 #import <Foundation/Foundation.h> @interface me : NSObje ...
- redo binlog
w https://dev.mysql.com/doc/refman/5.7/en/innodb-redo-log.html https://dev.mysql.com/doc/refman/5.7/ ...
- tomcat部署jenkins启动报错:insufficient free space available after evicting expired cache entries-consider increasing the maximum size of the cache.
在tomcat里面部署jenkins,启动tomcat,在jenkins上操作不久之后,jenkins就挂掉了,查看tomcat控制台,报内存溢出信息: 解决该问题方法,修改tomcat/bin目录下 ...
- 我的Android进阶之旅------>Android中adb install 安装错误常见列表
adb的安装过程分为传输与安装两步. 在出错后,adb会报告错误信息,但是信息可能只是一个代号,需要自己定位分析出错的原因. 下面是从网上找到的几种常见的错误及解决方法: 1.INSTALL_FAIL ...
- 1、hive安装详细步骤
一.环境准备 hadoop-2.7.2 java 1.7.0 apache-hive-1.2.1 mysql Hive配置文件介绍 •hive-site.xml hive的配置文件 •hiv ...
- loadrunner脚本篇——Run-time Settings之ContentCheck
运用场景(很少用到): ContentCheck的设置可用来让VuGen检测存在错误的站点页面.如果被测的Web应用没有使用自定义的错误页面,那么这里不用添加规则,因为LR在回放时候,可以默认的捕捉到 ...
- iOS 关于 Missing iOS Distribution signing identity for.... 等 打包 校验 出现的事故 处理经验
着实郁闷了一阵子,不知道为什么 证书和配置文件都没有问题 在Archieve后 validate 提示:"Missing iOS Distribution signing identity ...
- $python正则表达式系列(2)——re模块常用函数
本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...
- linux systemctl 指令 —— 阮一峰
Systemd 指令 原文链接如果有效,请点击原文链接查看.原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.ht ...