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. 成功解决:FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is

    问题原因: 包内出错,是h5py包 解决思路: 执行如下操作: pip -- install h5py==2.8.0rc1 注意:如果执行pip install h5py==2.8.0rc1 成功话, ...

  2. 增强for循环 java.util.ConcurrentModificationException

    Java中的Iterator功能比较简单,并且只能单向移动: (1) 使用方法iterator()要求容器返回一个Iterator.第一次调用Iterator的next()方法时,它返回序列的第一个元 ...

  3. apache poi根据模板导出excel

    需要预先新建编辑好一个excel文件,设置好样式. 编辑好输出的数据,根据excel坐标一一对应. 支持列表数据输出,列表中列合并. 代码如下: package com.icourt.util; im ...

  4. JS interview loop code

    //九九乘法表 document.write("<table width='600' border=0'>"); for(var i=1; i<=9; i++){ ...

  5. March 30 2017 Week 13 Thursday

    I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. On the day, March 12th 2017, I ...

  6. [USACO12FEB]牛券Cow Coupons

    嘟嘟嘟 这其实是一道贪心题,而不是dp. 首先我们贪心的取有优惠券中价值最小的,并把这些东西都放在优先队列里,然后看[k + 1, n]中,有些东西使用了优惠券减的价钱是否比[1, k]中用了优惠券的 ...

  7. [19/03/23-星期六] 容器_ 泛型Generics

    一.概念 生活中的容器不难理解,是用来容纳物体的,程序中的“容器”也有类似的功能,就是用来容纳和管理数据. 数组就是一种容器,可以在其中放置对象或基本类型数据. ---优势:是一种简单的线性序列,可以 ...

  8. Java读取classpath下的文件

    写Java程序时会经常从classpath下读取文件,是时候该整理一下了,并在不断深入的过程中,陆续补充上. 现在Java project 都以maven项目居多, 比如像下面这样的一个项目结构: 编 ...

  9. ubuntu14.04下搜狗拼音输入法不正常的解决方法

    在终端输入ibus-daemon –drx命令,可以解决该问题.

  10. 【luogu P3369 【模板】普通平衡树(Treap/SBT)】 模板 Scapegoat Tree

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...