Qt5.12实战之图形编程初识

演示效果:

 1.绘制条件:

1. 绘图设备-> QPainter

2.画笔->QPen  --->字体 (QFont)

3.画刷->QBrush-->自己定义画刷(QPixmap)

4.绘制事件->QPaintEvent

绘图步骤:

1.重写基类的虚函数 

 void paintEvent(QPaintEvent *event);

 2.在虚函数 void paintEvent(QPaintEvent *event)的实现函数体中进行绘图

 将当前窗口做为绘图设备

 QPainter p(this);

开始绘图 

 p.begin(this);

结束绘图

p.end();

直线绘制: 30为点A的x坐标,230为点A的y坐标 , 350为点B的x坐标,230为点B的y坐标

 p.drawLine(30,230,350,230);//画线

 绘制矩形:

1.设置画笔为红色 

 p.setPen(Qt::red);

2.绘制红色矩形: 10,10为矩形的x,y坐标  ,100,100为矩形的宽与高

 p.drawRect(10,10,100,100);

 绘制椭圆

设置画笔大小与颜色

p.setPen(QPen(Qt::green,5));

设置画刷 

p.setBrush(QBrush(Qt::yellow,Qt::Dense4Pattern));

使用画笔画椭圆并用画刷填充

 p.drawEllipse(130,10,250,200);

 

修改画笔样式画圆角矩形

QPen _pen;
    _pen.setStyle(Qt::DashDotLine);
    _pen.setWidth(10);
    _pen.setColor(QColor(255,0,0));
    _pen.setCapStyle(Qt::RoundCap);
    p.setBrush(Qt::NoBrush);
    p.setPen(_pen);
    p.drawRoundRect(300,300,100,100);

 

 

创建图片画刷 

QPixmap _pix(QApplication::applicationDirPath() + "/bg.jpg");
    int w = _pix.width();
    int h = _pix.height();
    _pix.scaled(w,h,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    QBrush imgBru(_pix);//图片画刷

使用图片画刷画矩形

 p.setBrush(imgBru);
    p.drawRect(400,400,w,h);
    p.setBrush(Qt::NoBrush);

 文本绘制:

 p.setPen(Qt::blue);
    p.setFont(QFont(QStringLiteral("微软雅黑"),36));
    p.drawText(rect(),Qt::AlignCenter,QStringLiteral("入侵吧!"));

 

绘图附加知识:

颜色类型转换:

 QColor c(255,0,255);
    QRgb rgb = qRgb(c.red(),c.green(),c.blue());//qcolor to qrgb

    QColor c1 = QColor(rgb);//qrgb to qcolor

    QString cstr= QString::number(rgb,16);
    QColor c2(cstr.toUInt(NULL,16));//qstring to qcolor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林宏权

有你的鼓励,我会更加努力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值