lesson10-QT二维图形绘制

一、 画图
1、QPainter
Qt中定义了几种绘图设备,如QWidget、QPainter、QPixmap等等。他们都继承自QPaintDevice
QPainter提供了许多高度优化的函数去完成GUI画图工作,可以画简单的或者复杂的图形。QPainter使用QWIdget的painter事件来画图,事件处理函数是paintEvent(QPaintEvent *e)
画图时首先要有一个QPainter实例
QPainter p;
启动画图功能,参数指定画图的父控件
p.begin(this);
开始画图
p.drawRect(QRect r);
关闭画图功能
p.end();
整个绘图过程都是在Qt事件中
QPainter提供一系列的函数可以让我们画出一些基本图形

2、QPen
在画图的时候,QPen用来画轮廓线。因此我们可以使用QPen来改变图形的轮廓,例如可以画实线、虚线、点线等等

改变QPainter的画笔:
QPainter p;
QPen pen;
p.setPen(pen);

改变画笔自身的属性
pen.setColor(Qt::white);   //颜色
pen.setStyle(Qt::SolidLine);//样式


3、QBrush
在画图的时候,QBrush可以填充图形,例如可以将图形填充为黄色、绿色等等

改变QPainter的画笔:
QPainter p;
QBrush brush;
p.setBrush(brush);

改变画笔自身的属性
brush.setColor(Qt::white); //颜色
brush.setStyle(Qt::CrossPattern);//样式

update()函数会产生一个画图事件
 

4、update
update()函数会产生一个画图事件,当串口被隐藏、部件失效的时候,这个函数会产生一个事件

点击(此处)折叠或打开

  1. #include "MyDraw.h"

  2. MyDraw::MyDraw()
  3. {
  4.     //控件初始化
  5.     btn = new QPushButton("更改");
  6.     colorBoxInit();
  7.     styleBoxInit();
  8.     drawWidgetInit();
  9.     //水平布局
  10.     QHBoxLayout *hLay = new QHBoxLayout();
  11.     hLay->addWidget(colorBox);
  12.     hLay->addWidget(styleBox);
  13.     hLay->addWidget(btn);
  14.     //垂直布局
  15.     QVBoxLayout *vLay = new QVBoxLayout();
  16.     vLay->addLayout(hLay);
  17.     vLay->addWidget(drawWidget);
  18.     //加载布局
  19.     setLayout(vLay);
  20.     //绑定信号和槽函数
  21.     connect(btn, SIGNAL(clicked()), this, SLOT(changeSlot()));
  22.     //重定义窗口大小
  23.     resize(500,500);
  24. //    drawWidget->setPalette(QPalette(Qt::white));
  25. }
  26. void MyDraw::colorBoxInit()
  27. {
  28.     colorBox = new QComboBox();
  29.     QStringList color;
  30.     color"红""黄""蓝""绿""黑";
  31.     colorBox->addItems(color);
  32. }
  33. void MyDraw::styleBoxInit()
  34. {
  35.     styleBox = new QComboBox();
  36.     QStringList style;
  37.     style"实线""虚线""点划线";
  38.     styleBox->addItems(style);
  39. }
  40. void MyDraw::drawWidgetInit()
  41. {
  42.     drawWidget = new QWidget;
  43.     drawWidget->resize(400, 400);
  44. }

  45. void MyDraw::changeSlot()
  46. {
  47.     switch(colorBox->currentIndex())
  48.     {
  49.         case 0:
  50.             pen.setColor(Qt::red);
  51.             break;
  52.         case 1:
  53.             pen.setColor(Qt::yellow);
  54.             break;
  55.         case 2:
  56.             pen.setColor(Qt::blue);
  57.             break;
  58.         case 3:
  59.             pen.setColor(Qt::black);
  60.             break;
  61.         default:
  62.             break;
  63.     }
  64.     switch(styleBox->currentIndex())
  65.     {
  66.         case 0:
  67.             pen.setStyle(Qt::SolidLine);
  68.             break;
  69.         case 1:
  70.             pen.setStyle(Qt::DashLine);
  71.             break;
  72.         case 2:
  73.             pen.setStyle(Qt::DotLine);
  74.             break;
  75.         default:
  76.             break;
  77.     }
  78.     //event
  79.     update();
  80. }

  81. //绘图时间
  82. void MyDraw::paintEvent(QPaintEvent *e)
  83. {
  84.     p.begin(this);
  85.     p.setPen(pen);
  86.     QBrush brush;
  87.     brush.setStyle(Qt::CrossPattern);
  88.     p.setBrush(brush);
  89.     QRect rect(100, 100, 200, 400);
  90.     p.drawEllipse(rect);
  91.     p.end();
  92. }




二、坐标系变换
Qt的坐标系由QPainter控制,同时也由QPaintDevice控制。
QPaintDevice是物理坐标,QPainter是逻辑坐标。逻辑坐标和物理坐标默认是一致的,但是也可以变换。Qt坐标原点在左上角,x轴向右增长,y轴上下增长

QPainter提供了一些变换函数,可以实现坐标的变换
1)tanslate(x,y) 平移
2)scale(m,n)缩放
3)shear(x,y)扭曲
4)rotate(m)旋转

点击(此处)折叠或打开

  1. #include "MyDraw.h"

  2. MyDraw::MyDraw()
  3. {
  4.     //控件初始化
  5.     btn = new QPushButton("更改");
  6.     colorBoxInit();
  7.     styleBoxInit();
  8.     drawWidgetInit();
  9.     //水平布局
  10.     QHBoxLayout *hLay = new QHBoxLayout();
  11.     hLay->addWidget(colorBox);
  12.     hLay->addWidget(styleBox);
  13.     hLay->addWidget(btn);
  14.     //垂直布局
  15.     QVBoxLayout *vLay = new QVBoxLayout();
  16.     vLay->addLayout(hLay);
  17.     vLay->addWidget(drawWidget);
  18.     //加载布局
  19.     setLayout(vLay);
  20.     //绑定信号和槽函数
  21.     connect(btn, SIGNAL(clicked()), this, SLOT(changeSlot()));
  22.     //重定义窗口大小
  23.     resize(500,500);
  24.     drawWidget->setPalette(QPalette(Qt::white));
  25. }
  26. void MyDraw::colorBoxInit()
  27. {
  28.     colorBox = new QComboBox();
  29.     QStringList color;
  30.     color"红""黄""蓝""绿""黑";
  31.     colorBox->addItems(color);
  32. }
  33. void MyDraw::styleBoxInit()
  34. {
  35.     styleBox = new QComboBox();
  36.     QStringList style;
  37.     style"实线""虚线""点划线";
  38.     styleBox->addItems(style);
  39. }
  40. void MyDraw::drawWidgetInit()
  41. {
  42.     drawWidget = new QWidget;
  43.     drawWidget->resize(400, 400);
  44. }

  45. void MyDraw::changeSlot()
  46. {
  47.     switch(colorBox->currentIndex())
  48.     {
  49.         case 0:
  50.             pen.setColor(Qt::red);
  51.             break;
  52.         case 1:
  53.             pen.setColor(Qt::yellow);
  54.             break;
  55.         case 2:
  56.             pen.setColor(Qt::blue);
  57.             break;
  58.         case 3:
  59.             pen.setColor(Qt::black);
  60.             break;
  61.         default:
  62.             break;
  63.     }
  64.     switch(styleBox->currentIndex())
  65.     {
  66.         case 0:
  67.             pen.setStyle(Qt::SolidLine);
  68.             break;
  69.         case 1:
  70.             pen.setStyle(Qt::DashLine);
  71.             break;
  72.         case 2:
  73.             pen.setStyle(Qt::DotLine);
  74.             break;
  75.         default:
  76.             break;
  77.     }
  78.     update();
  79. }

  80. //绘图时间
  81. void MyDraw::paintEvent(QPaintEvent *e)
  82. {
  83.     //构造一个矩形
  84.     QRect rect(100,100,300,400);
  85.     //开始画图
  86.     p.begin(this);
  87.     //指定一个画刷
  88.     QBrush brush;
  89.     brush.setStyle(Qt::CrossPattern);
  90.     //加载画笔和画刷
  91.     p.setPen(pen);
  92.     p.setBrush(brush);
  93.     //画椭圆
  94.     p.drawEllipse(rect);

  95.     p.translate(100, 0);
  96.     p.drawEllipse(rect);
  97.     p.scale(0.5, 0.5);
  98.     p.drawEllipse(rect);

  99.     p.end();
  100. }



三、QImage
Qt提供了4个处理图像的类:QImage、QPixmap、QBitmap、QPicture

QImage可以直接存储像素数据
QPixmap用来在屏幕上显示图片
QPicture可以记录和重放QPainter类
QBitmap用来处理位图,继承自QPixmap

QImage支持的图像可以是单位、8位、32位、alpha混合格式
加载图像可以使用load()函数、loadFromData()函数;save()可以保存图像
size() height() width()可以获取图像的大小等信息
format()可以取出图像的格式
convertToFormat()可以转换图片格式

使用QPainter的drawImage方法可以将QImage图案画到窗口

点击(此处)折叠或打开

  1. #include "myImage.h"

  2. myImage::myImage()
  3. {
  4.     //控件初始化
  5.     openBtn = new QPushButton("打开");
  6.     saveBtn = new QPushButton("保存");
  7.     image.load("/cut.jpg");
  8.     QHBoxLayout *hLay = new QHBoxLayout();
  9.     hLay->addWidget(openBtn);
  10.     hLay->addWidget(saveBtn);
  11.     //布局
  12.     QVBoxLayout *vLay = new QVBoxLayout();
  13.     vLay->addLayout(hLay);
  14.     QWidget *widget = new QWidget();
  15.     widget->resize(500,500);
  16.     vLay->addWidget(widget);
  17.     //加载布局
  18.     setLayout(vLay);
  19.     resize(600,600);
  20.     //绑定信号
  21.     connect(openBtn, SIGNAL(clicked()), this, SLOT(openSlot()));
  22.     connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
  23. }
  24. //槽函数
  25. void myImage::openSlot()
  26. {
  27.     //获取文件名字
  28.     QString str = QFileDialog::getOpenFileName(this, "打开文件", "/", "All File(*.*)");
  29.     //加载图片
  30.     image.load(str);
  31.     update();
  32. }
  33. //槽函数
  34. void myImage::saveSlot()
  35. {
  36.     bool ok;
  37.     //获取输入的信息
  38.     QString str = QInputDialog::getText(this, "输入对话框", "请输入名字", QLineEdit::Normal, "wj", &ok);

  39.     //保存图像
  40.     image.save(str + ".png");
  41. }
  42. void myImage::paintEvent(QPaintEvent *e)
  43. {
  44.     QPainter p(this);
  45.     p.drawImage(100,100,image);
  46. }




 




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值