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. 使用本地计划任务定时关闭azure虚拟机

    本文包含以下内容 前提条件 如何实现定时关闭虚拟机 前提条件 Controller 机器上必须安装 Azure PowerShell,并且要在 PowerShell 里登录一次 Azure, 请参见: ...

  2. 一张图看懂微软Power BI系列组件

    一.Power BI简介 Power BI是微软最新的商业智能(BI)概念,它包含了一系列的组件和工具.话不多说,直接上图吧: Power BI的核心理念就是让我们用户不需要强大的技术背景,只需要掌握 ...

  3. 11GR2 双节点RAC 配置单节点DG

    只记录主要步骤,供大家参考: RAC 搭建单节点 DG 1 修改源数据库开启归档和force loggingalter system set shared_servers=0; alter datab ...

  4. 通过describe命令学习Kubernetes的pod属性详解

    我们可以首先使用kubectl get pods命令得到pod列表,比如我们想研究pod nginx-storage-pod的明细: 使用命令kubectl describe pod nginx-st ...

  5. Jupyter notebook 的一个问题

    Traceback (most recent call last): File , in get value = obj._trait_values[self.name] KeyError: 'all ...

  6. luogu4566 [Vani有约会]雨天的尾巴

    题目 线段树合并的板子题目了,写一写对线段树合并的理解 首先线段树合并就是把一大堆权值线段树合并起来的算法 尽管复杂度看起来并不是非常科学,但是确是非常优秀的\(O(nlogn)\) 主要的写法两种 ...

  7. 【转】jQuery源码分析-03构造jQuery对象-源码结构和核心函数

    作者:nuysoft/高云 QQ:47214707 EMail:nuysoft@gmail.com 毕竟是边读边写,不对的地方请告诉我,多多交流共同进步.本章还未写完,完了会提交PDF. 前记: 想系 ...

  8. EJB3 调用的存储过程

    要调用存储过程,我们可以通过 EntityManager 对象的 createNativeQuery()方法执行 SQL 语句 (注意:这里说的是SQL 语句,不是 EJB3 QL), 调用存储过程的 ...

  9. 关于SQLNET.AUTHENTICATION_SERVICES= (NTS) 的解释

    原文转自:http://www.360doc.com/content/12/0207/12/3446769_184740592.shtml       标题所代表的意思为 使用操作系统本地验证,一般不 ...

  10. UICollectionViewCell「居左显示」

    UICollectionViewCell「居左显示」 准备: 1.UICollectionView Left Aligned Layout 一款UICollectionView居左显示的约束点击下载_ ...