Qt——绘图
1.涉及类
QPainter QPaintEngine QPaintDevice
作为绘图的使用者,只需要关注 QPainter 和 QPaintDevice
2.QPainter
使用 QPainter 进行绘图
常用API
setPen, setBrush, setDevice
Pen, Brush : 又有 setColor 、setStyle
3. QDevice
通过与 QDevice 的继承关系知道能在哪里绘图。
常见 绘图设备
QPixmap : 对屏幕显示进行优化,与平台相关
QBitmap : 继承与QPixmap,只有黑白两色,省资源。
QImage :与平台无关,能进行图片修改,能线程绘图
QPicture:二进制文件保存绘图状态。
//绘图设备, 400*300
QPixmap pixmap(, );
QPainter p(&pixmap);
//填充白色背景色
//p.fillRect(0, 0, 400, 300, QBrush(Qt::white));
pixmap.fill(Qt::white);
p.drawPixmap(, , , , QPixmap("../Image/face.png"));
//保存图片
pixmap.save("../pixmap.jpg");
//创建一个绘图设备,QImage::Format_ARGB32背景是透明
QImage image(, , QImage::Format_ARGB32);
QPainter p;
p.begin(&image);
//绘图
p.drawImage(, , QImage("../Image/face.png"));
//对绘图设备前50个像素点进行操作
; i < ; i++)
{
; j < ; j++)
{
image.setPixel(QPoint(i, j), qRgb(, , ));
//image.pixel(QPoint(i, j));
}
}
p.end();
image.save("../image.png");
QPicture picture;
QPainter p;
p.begin(&picture);
p.drawPixmap(, , , , QPixmap("../Image/face.png"));
p.drawLine(, , , );
p.end();
//保存的是二进制文件
picture.save("../picture.png");
QPicture pic;
pic.load("../picture.png"); //加载文件
QPainter p(this);
p.drawPicture(, , pic);
QPixmap与QImage的互相转换
QPainter p(this);
QPixmap pixmap;
pixmap.load("../Image/face.png");
//QPixmap -> QImage
QImage tempImage = pixmap.toImage();
p.drawImage(, , tempImage);
QImage image;
image.load("../Image/face.png");
//QImage -> QPixmap
QPixmap tempPixmap = QPixmap::fromImage(image);
p.drawPixmap(, , tempPixmap);
4.绘制无边框图片
重要API:
设置无边框
setWindowFlags
setAttribute
globalPos
frameGeometry
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QMouseEvent>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//去窗口表框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
//把窗口背景设置为透明
setAttribute(Qt::WA_TranslucentBackground);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawPixmap(, , QPixmap("../Image/sunny.png"));
}
void Widget::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::RightButton)
{
//如果是右键
close();
}
else if(e->button() == Qt::LeftButton)
{
//求坐标差值
//当前点击坐标-窗口左上角坐标
p = e->globalPos() - this->frameGeometry().topLeft();
}
}
void Widget::mouseMoveEvent(QMouseEvent *e)
{
if(e->buttons() & Qt::LeftButton)
{
move(e->globalPos() - p);
}
}
Qt——绘图的更多相关文章
- Qt绘图
Qt绘图的设置 QPainter::Antialiasing // 反锯齿 QPainter::TextAntialiasing // 文字反锯齿 QPainter::SmoothPixmapTran ...
- Qt绘图之QGraphicsScene QGraphicsView QGraphicsItem详解
Graphics View提供了一个界面,它既可以管理大数量的定制2D graphical items,又可与它们交互,有一个view widget可以把这些项绘制出来,并支持旋转与缩放.这个柜架也包 ...
- 简单的QT绘图程序(把全部的点都记录下来,然后在paintEvent里使用drawLine函数进行绘制,貌似效率很低。。。)
当初在学MFC时,最经典的入门实例就是绘图程序,其作用相当于Console Application 下的Hello World了吧. 如今入手QT,不免怀旧,于是也写了一个绘图程序,虽然简单,却也是入 ...
- 界面编程之QT绘图和绘图设备20180728
/*******************************************************************************************/ 一.绘图 整 ...
- Qt 绘图与动画系统
Qt 提供了内置的绘图系统以及独立的QtOpenGL模块提供对OpenGL的支持.Qt提供了基于状态机的QPainter系统和面向对象的Graphics View系统. QPainter 基于状态机的 ...
- Qt 绘图(QBitmap,QPixmap,QImage,QPicture)
QPainter绘图绘图设备(QPixmap,QImage,QBitmap,QPicture) 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状 ...
- Qt绘图学习(1)
paintEvent()被调用的时机;1.当窗口第一次被show()的时候,Qt程序会自动产生一个绘图事件,调用绘图事件:2.重新调整窗口部件大小的时候,系统也会产生一个绘制事件.3.当窗口部件被其他 ...
- Qt绘图浅析与实例
1. Qt5位置相关函数 Q提供了很多关于获取窗体位置及显示区域大小的函数,如x().y()和pos().rect().size().geometry()等,统称为"位置相关函数" ...
- Qt: 绘图基础(非常简洁明了,全面)
QPainter 能绘制: point, line, rectangle, ellipse, arc, chord, polygon, pie segment, Bezier curve, QPixm ...
随机推荐
- python获取硬件信息模块
https://github.com/redhat-cip/hardware https://github.com/rdobson/python-hwinfo https://github.com/r ...
- liunx增强命令
查找命令 grep 格式:grep [option] pattern [file] 实例: ps -ef | grep sshd 查找指定 ssh 服务进程 ps -ef | grep sshd | ...
- ZT C++关键字new学习
http://blog.csdn.net/waken_ma/article/details/4007914 C++关键字new学习 很多新手对C++关键字new可能不是很了解吧,今天我一起来学习一下. ...
- 设置IE浏览器的默认主页
实现效果: 知识运用: RegistryKey类的GetValue方法 public Object GetValue (string name , Object defaultValue) name ...
- java反射 反射构造函数 报 wrong number of arguments 错误
package com; import java.lang.reflect.Constructor; public class Person { public Person() { } public ...
- AngularJS 控制器函数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 面向对象编程——class继承
在上面的章节中我们看到了JavaScript的对象模型是基于原型实现的.特点是简单,缺点是理解起来比传统类-实例模型要困难,最大的缺点是继承的实现需要编写大量代码,并且需要正确实现原型链. 新的关键字 ...
- 课时60.CSS的固定格式(掌握)
CSS就是用来设置样式的,美化界面的 如何验证? 打开一个京东首页 删除掉css样式 发现页面变得非常难看 由此我们验证了一个说法,css就是用来美化界面的 1.格式: <style type= ...
- 买手机时几GB+几GB啥意思
48GB 就是你每次下载手机软件呀.浏览图片呀.这些东西都放在48G里.你每次查看手机内存,就会看到你的48G用了多少.但是你什么时候看到你的4GB用了多少,都是那些360加速球呀提示你手机内存占用过 ...
- DBF导入到Oracle数据库
今天我遇到了一个需求,是将一个DBF文件导入到Oracle库中,之前一直使用的是公司提供的迁移工具,但是不知道怎么回事今天一直报DBF文件无法访问之类的错误,尝试多次之后,一气之下弃之不用,另寻他法. ...