Qt5/QPainter/画家

 

目录

 

1.绘制文字

1.1案例

1.2代码 

2.画线 

2.1案例

 2.2代码

3.绘制矩形

3.1代码

4.绘制椭圆和圆

4.1椭圆

4.2圆

5.圆弧

5.1案例:

5.2代码 

6.扇形

6.1案例 

6.2代码:

7.更改绘画的颜色和粗细

7.1案例 

​编辑 7.2代码


1.绘制文字

1.1案例

通过三种不同的绘制方式进行绘制

1.2代码 

void Widget::paintEvent(QPaintEvent *event)
{
    //实列化一个QPainter对象,this表示在Widget的这个窗口进行会话
    QPainter painter(this);

    painter.setPen(Qt::blue);//设置画笔的颜色
    painter.setFont(QFont("Arial", 20));//设置字体,"Arial"为字体类型,30为要设置的字体大小

    //在窗口的坐标(x:40,y:60)的位置绘制"小张"
    //drawText(int x, int y, const QString &text)
    painter.drawText(40,60,"小张");
    
    //在矩形rect(窗口),的中间位置(Qt::AlignCenter),绘制"Qt案例"
    //drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect = nullptr)
    //drawText(const QRect &rectangle, int flags, const QString &text, QRect *boundingRect = nullptr)
     painter.drawText(rect(), Qt::AlignCenter, "Qt案例");

    //在窗口的坐标(x:150,y:200)的位置建立一个长100宽100的矩形,在这个矩形的中间绘制"Qt5","Qt::AlignCenter"表示在矩形的中间绘制
    //drawText(int x, int y, int width, int height, int flags, const QString &text, QRect *boundingRect = nullptr)
    painter.drawText(150,200,100,100,Qt::AlignCenter, "Qt5");
}

2.画线 

2.1案例

通过3种不同的方法绘制同一种线 

 

 2.2代码

void Widget::paintEvent(QPaintEvent *event)
{
    //实列化一个QPainter对象,this表示在Widget的这个窗口进行会话
    QPainter painter(this);

    painter.setPen(Qt::blue);//设置画笔的颜色
    painter.setFont(QFont("Arial", 20));//设置字体,"Arial"为字体类型,30为要设置的字体大小

    painter.drawLine(10,200,300,20);
    painter.drawLine(QLine(10,200,300,20));
    painter.drawLine(QPoint(10,200),QPoint(300,20));
}

3.绘制矩形

3.1代码

    方式1:
    QRect rectangle(200,100,220,120);//在(200,100)的位置实列化一个长220矩形宽120的矩形
    painter.drawRect(rectangle);
    方式2:
    painter.drawRect(200,100,220,120);//直接绘制

4.绘制椭圆和圆

4.1椭圆

    //方法1
    QRect rectangle(200,100,220,120);
    painter.drawEllipse(rectangle);
    
    //在坐标(300,300)为中心处绘制
    painter.drawEllipse(300,300,200,100);

4.2圆

当椭圆的短轴的长度与长轴长的长度一样时即可画圆

   //方法1
    QRect rectangle(200,100,120,120);
    painter.drawEllipse(rectangle);
    
    //在坐标(300,300)为中心处绘制
    painter.drawEllipse(300,300,200,200);

5.圆弧

起始角和伸缩角必须以1/16度指定,即一个完整的圆等于5760(16*360),可以理解为Qt将我们认知中的1°又细分了16份。 角度的正值表示逆时针方向,负值表示顺时针方向。零度在3点钟方位。

5.1案例:

5.2代码 

    QRect rectangle(200,100,220,180);
    painter.drawArc(rectangle,30*16,120*16);

6.扇形

扇形绘制与弧形类似

6.1案例 

6.2代码:

    QRect rectangle(200,100,220,180);
    painter.drawPie(rectangle,30*16,120*16);

7.更改绘画的颜色和粗细

7.1案例 

 7.2代码

void Widget::paintEvent(QPaintEvent *event)
{
    //实列化一个QPainter对象,this表示在Widget的这个窗口进行会话
    QPainter painter(this);
    //使加粗后的线条看起来更光滑
    painter.setRenderHint(QPainter::Antialiasing,true);
    QPen pen(Qt::red,7);//7为线条宽度即宽度为7个像素点
    painter.setPen(pen);
    QRect rectangle(200,100,220,180);
    painter.drawPie(rectangle,30*16,120*16);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值