QT从入门到实战完整版 P38 P39

手动调用绘图事件

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    //绘图事件
    void paintEvent(QPaintEvent *);

    int posX=0;
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QBrush>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //点击移动按钮
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        //如果要手动调用绘图事件,用update更新
        posX+=20;
        update();
    });
}
//绘图事件
void Widget::paintEvent(QPaintEvent *)
{
    /*

    //实例化画家类 this指定的是绘图的设备
    QPainter painter(this);
    //设置画笔
    QPen pen(QColor(255,0,0));
    //设置画笔宽度
    pen.setWidth(3);
    //设置画笔风格
    pen.setStyle(Qt::DotLine);
    //让画家使用这个笔
    painter.setPen(pen);

    //设置画刷
    //QBrush brush(QColor(0,255,0));
    QBrush brush(Qt::green);
    //设置画刷风格
    brush.setStyle(Qt::Dense7Pattern);
    //让画家使用画刷
    painter.setBrush(brush);

    painter.drawLine(QPoint(0,0),QPoint(100,100)); //画线
    painter.drawEllipse(QPoint(100,100),50,50);//画圆(椭圆100 50)
    painter.drawRect(QRect(20,20,50,50)); //画矩形
    painter.drawText(QRect(10,200,150,50),"好好学习,天天向上");//画文字
    */

    ////高级设置
    /* QPainter painter(this);

    //圆形
    painter.drawEllipse(QPoint(100,50),50,50);
    //设置 抗锯齿能力 效率较低
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawEllipse(QPoint(250,50),50,50);
    */
    /*
    //矩形
    painter.drawRect(QRect(20,20,50,50));
    painter.translate(100,0);//移动画家
    //保存画家状态
    painter.save();
    painter.drawRect(QRect(20,20,50,50));
    painter.translate(100,0);//移动画家
    painter.restore();//还原画家保存状态
    painter.drawRect(QRect(20,20,50,50));
*/
    ////利用画家画资源图片////
    QPainter painter(this);
    //如果超出了屏幕 从0开始
    if(posX>this->width())
    {
        posX=0;
    }
    painter.drawPixmap(posX,0,QPixmap(":/rec/jb.jpg"));
}
Widget::~Widget()
{
    delete ui;
}

绘图设备(未实现)

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QImage>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    //绘图事件
    void painterEvent(QPaintEvent *);
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QBrush>
#include <QPixmap>
#include <QPainter>
#include <QImage>
#include <QPicture>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    /*
    //Pixmap绘图设备 专门为平台做了显示的优化
    QPixmap pix(300,300);
    //填充颜色
    pix.fill(Qt::white);
    //声明画家
    QPainter painter(&pix);
    painter.setPen(QPen(Qt::green));
    painter.drawEllipse(QPoint(150,150),100,100);

    //保存
    pix.save("E:\\pix.png");
*/
    /*
    //image 绘图设备 可以对像素进行访问
    QImage img(300,300,QImage::Format_RGB32);
    img.fill(Qt::white);
    QPainter painter(&img);
    painter.setPen(QPen(Qt::blue));
    painter.drawEllipse(QPoint(150,150),100,100);
    //保存
    img.save("E:\\img.png");
*/
    //QPicture 绘图设备 可以记录和重现绘图指令
    QPicture pic;
    QPainter painter;
    painter.begin(&pic);
    painter.setPen(QPen(Qt::cyan));
    painter.drawEllipse(QPoint(150,150),100,100);
    painter.end();//结束画画
    //保存到磁盘
    pic.save("E:\\pic.zt");
}
//绘图事件
void Widget::painterEvent(QPaintEvent *)
{
    /*
    //!没有实现好
    QPainter painter(this);
    //利用QImage 对像素进行修改
    QImage img;
    img.load(":/rec/x1.jpg");
    //修改像素点
    for(int i=50;i<100;i++)
    {
        for(int j=50;j<100;j++)
        {
            QRgb value=qRgb(255,0,0);
            img.setPixel(i,j,value);
        }
    }
    painter.drawImage(0,0,img);
*/
    QPainter painter(this);
    //重现QPicture绘图指令
    QPicture pic;
    pic.load("E:\\pic.zt");
    painter.drawPicture(0,0,pic);
}
Widget::~Widget()
{
    delete ui;
}
posted @ 2023-07-24 17:08  Vijurria  阅读(40)  评论(0编辑  收藏  举报