一起看代码来玩玩QT之07 Painter


MyWidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);

    void paintEvent(QPaintEvent *);

signals:

public slots:

};

#endif // MYWIDGET_H

MyWidget.cpp

#include "MyWidget.h"
#include <QPainter>
#include <QPixmap>
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
}

void MyWidget::paintEvent(QPaintEvent *)   //调用:1 窗口初始化 2update 3 系统认为重绘时
{
    QPixmap pixmap(size()); //相当于小车打包下列 东西

    QPainter p(&pixmap);

 //   p.translate(100, 100);  //简单的坐标平移变换
    //p.scale();
    p.setRenderHint(QPainter::Antialiasing);  // xiao jichi gong neng!!
    QTransform transform;    // 复杂的坐标平移变换
    transform.translate(50,50);
    transform.rotate(30);  //旋转
   // transform.scale(.5, .5);//缩放
    p.setTransform(transform);
#if 1
    QTransform transform2;
    transform2.scale(.5, .5);
    p.setTransform(transform2, true);  //can combine
#endif

    p.drawLine(QPoint(0, 0), QPoint(100, 100));   //画

 //   p.translate(-100, -100);
    p.setPen(QPen(Qt::red, 2, Qt::DashLine)); //   设置 pen brush刷 font
    p.setBrush(Qt::yellow);
    p.setFont(QFont("aaa", 40, 700, true));

    p.drawEllipse(QPoint(95, 333), 50, 50);
    p.drawText(QPoint(300, 50), "Hello world");
 //   p.drawPixmap(QPoint(40, 40), QPixmap("../aaa.png"));
  //  p.drawRect(QRect(40, 60, 100, 50));
    p.drawRoundRect(QRect(40, 60, 100, 50));

    p.end();

    p.begin(this);
    p.drawPixmap(0, 0, pixmap);

}

#include <QApplication>
int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    MyWidget w;
    w.show();

    return app.exec();
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值