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——绘图的更多相关文章

  1. Qt绘图

    Qt绘图的设置 QPainter::Antialiasing // 反锯齿 QPainter::TextAntialiasing // 文字反锯齿 QPainter::SmoothPixmapTran ...

  2. Qt绘图之QGraphicsScene QGraphicsView QGraphicsItem详解

    Graphics View提供了一个界面,它既可以管理大数量的定制2D graphical items,又可与它们交互,有一个view widget可以把这些项绘制出来,并支持旋转与缩放.这个柜架也包 ...

  3. 简单的QT绘图程序(把全部的点都记录下来,然后在paintEvent里使用drawLine函数进行绘制,貌似效率很低。。。)

    当初在学MFC时,最经典的入门实例就是绘图程序,其作用相当于Console Application 下的Hello World了吧. 如今入手QT,不免怀旧,于是也写了一个绘图程序,虽然简单,却也是入 ...

  4. 界面编程之QT绘图和绘图设备20180728

    /*******************************************************************************************/ 一.绘图 整 ...

  5. Qt 绘图与动画系统

    Qt 提供了内置的绘图系统以及独立的QtOpenGL模块提供对OpenGL的支持.Qt提供了基于状态机的QPainter系统和面向对象的Graphics View系统. QPainter 基于状态机的 ...

  6. Qt 绘图(QBitmap,QPixmap,QImage,QPicture)

    QPainter绘图绘图设备(QPixmap,QImage,QBitmap,QPicture) 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状 ...

  7. Qt绘图学习(1)

    paintEvent()被调用的时机;1.当窗口第一次被show()的时候,Qt程序会自动产生一个绘图事件,调用绘图事件:2.重新调整窗口部件大小的时候,系统也会产生一个绘制事件.3.当窗口部件被其他 ...

  8. Qt绘图浅析与实例

    1. Qt5位置相关函数 Q提供了很多关于获取窗体位置及显示区域大小的函数,如x().y()和pos().rect().size().geometry()等,统称为"位置相关函数" ...

  9. Qt: 绘图基础(非常简洁明了,全面)

    QPainter 能绘制: point, line, rectangle, ellipse, arc, chord, polygon, pie segment, Bezier curve, QPixm ...

随机推荐

  1. 通过 Powershell 来替换 ARM 模式下虚拟机的网络接口

    需求描述 客户在部署完 ARM 模式的虚拟机以后,由于误操作在虚拟机内部禁用了网卡导致远程访问虚拟机受到限制,以下是通过 Powershell 命令来替换原有虚拟网络接口实现虚拟网卡重置功能. Not ...

  2. 聚合不应出现在 UPDATE 语句的集合列表中

    修改语句: update A set WZCount=ISNULL(WZCount,0)+(select SUM(WZCount) from T_PM_OutStock_SUB where Mater ...

  3. Oracle数据库克隆后temp文件因路径变化无法找到问题

    Oracle数据库克隆后temp文件因路径变化无法找到出现如下报错Errors in filexxxx.trc:ORA-01157: cannot identify/lock data file xx ...

  4. cross entropy与logistic regression

    维基上corss entropy的一部分 知乎上也有一个类似问题:https://www.zhihu.com/question/36307214 cross entropy有二分类和多分类的形式,分别 ...

  5. C# .Net Framework4.5中配置和使用managedCUDA及常见问题解决办法

    主要参考英文帖子.我就不翻译了哈.很容易懂的. 先说明我的运行平台: 1.IDE:Visual Studio 2012 C# .Net Framework4.5,使用默认安装路径: 2.显卡类型:NV ...

  6. 【办公-Word-VB】人民币大写转换-带完整注释

    完整代码见:我的CSDN博客 -------------------- 应公司财务人员的请求,需在Word中做个:输入阿拉伯数字,自动转换成大写,并填充到Word控件中对应的亿.万.千控件格子的功能, ...

  7. detection and segmentation

    Relation Networks for Object Detection    https://arxiv.org/abs/1711.11575 Towards High Performance ...

  8. 跨交换机的VLAN划分实验

    实验涉及命令以及知识补充 大部分命令和上一篇相同,本篇介绍不同的收获. 上一篇链接: https://www.cnblogs.com/Coeus-P/p/9122462.html 退出命令 ctrl+ ...

  9. sql*plus

    [sql*plus创建txt文档编辑sql语句] (1)创建一个txt,命名doc SQL> ed  doc;          /*ed   文件名*/ (2)在doc.txt文件编辑sql语 ...

  10. vue、html与iframe html事件相互调用

    一.html文件中引入的iframe标签 1.在父html中调用子iframe html 中的事件 通过contentwindow属性 document.getElementById("my ...