Qt--canvas画图(5.12)

  • 1 介绍
  • 2 使用流程
  • 3 简单示例
    • 3.1 简单线、图形、文本、渐变、尺子
    • 3.2 选色绘画(转【参2】)
    • 3.3 简单鼠标绘制(转【参2】)
    • 3.4 绘制长方形(转【参2】)
  • 参考

1 介绍

画布项目允许绘制直线和曲线、简单和复杂的形状、图形和引用的图形图像。它还可以添加文本、颜色、阴影、渐变和模式,并执行低级像素操作。画布输出可以保存为图像文件或序列化为URL。

2 使用流程

  • 装载画笔或者填充模式
  • 创建绘制路径
  • 使⽤画笔或者填充绘制路径

3 简单示例

3.1 简单线、图形、文本、渐变、尺子

注: ctx.closePath();画直线使用时会出问题,线变长

import QtQuick 2.12
import QtQuick.Window 2.12Window {visible: truewidth: 640height: 640title: qsTr("Hello World")Canvas {id: mycanvaswidth: parent.widthheight: parent.heightonPaint: {var ctx = getContext("2d");// a red short linectx.strokeStyle = "red"ctx.lineWidth = 2ctx.beginPath()ctx.moveTo(50, 50)ctx.lineTo(150, 50)ctx.stroke()ctx.beginPath()ctx.strokeStyle = "grey"ctx.lineWidth = 6ctx.moveTo(50, 100)ctx.lineTo(150, 100)ctx.stroke()ctx.beginPath()ctx.lineWidth = 2// 填充ctx.fillStyle = "pink";ctx.fillRect(50, 150, 100, 50);// 描边ctx.strokeStyle = "red";ctx.strokeRect(50, 300, 100, 50);// 画圆ctx.strokeStyle = "grey";ctx.arc(100, 500, 50, 0, Math.PI*2);ctx.stroke();// 渐变var gradient = ctx.createLinearGradient(0, 50, 0, 100)gradient.addColorStop(0, "blue")gradient.addColorStop(0.5, "lightsteelblue")ctx.fillStyle = gradientctx.fillRect(200, 50, 100, 50)gradient = ctx.createLinearGradient(200, 0, 300, 0)gradient.addColorStop(0, "blue")gradient.addColorStop(0.5, "lightsteelblue")ctx.fillStyle = gradientctx.fillRect(200, 150, 100, 50)// 文本ctx.fillStyle = "green"ctx.strokeStyle = "blue"ctx.fontSize = 20ctx.font = "bold 26px Arial"var text = "qter.org";context.fillText(text, 200, 250)context.strokeText(text, 200, 300)ctx.font = "bold 12px Arial"draw(ctx)}}function draw(ctx ) {var config = {rulerWidth: 250,rulerHeight: 50,size: 5, // the total num of scalex: 350,y: 350,w: 5, // scale line intervalh: 10 // base length of the scale line}var size = (config.size || 100) * 10 + 1var x = config.xvar y = config.y - 1var w = 5 || 5 // width of the scale linevar h = 10 || 10var offset = 3ctx.fillStyle = "#F5DEB3"ctx.fillRect(x, y - config.rulerHeight, config.rulerWidth, config.rulerHeight);ctx.fillRect(x - config.rulerHeight, y, config.rulerHeight, config.rulerWidth);ctx.fillStyle = "#111"ctx.strokeStyle = '#333'ctx.lineWidth = 1ctx.font = 13// horizontal rulerfor (var i = 0; i < size; i++) {ctx.beginPath()ctx.moveTo(x + i * w, y)// long scale lineif (i % 10 == 0) {offset = (String(i / 10).length * 6 / 2)ctx.fillText(i / 10, x + i * w - offset + 10 , y - h * 1.2);ctx.lineTo(x + i * w, y - h * 2)} else {// mid scale line / short scale linectx.lineTo(x + i * w, y - (i % 5 === 0 ? 1 : 0.6) * h)}ctx.stroke()}// vertical rulerfor ( i = 0; i < size; i++) {ctx.beginPath()ctx.moveTo(y, x + i * w)// long scale lineif (i % 10 == 0) {offset = (String(i / 10).length * 6 / 2)ctx.lineTo( y - h * 2, x + i * w)ctx.fillText(i / 10, y - h * 1.2 - 5, x + i * w - offset + 15);} else {// mid scale line / short scale linectx.lineTo(y - (i % 5 === 0 ? 1 : 0.6) * h, x + i * w)}ctx.stroke()}}
}

3.2 选色绘画(转【参2】)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12Window {visible: truewidth: 640height: 480title: qsTr("Hello World")//定位元素,画四个带颜色的正方形Row {id: colorToolsanchors {horizontalCenter: parent.horizontalCentertop: parent.toptopMargin: 8}property color paintColor: "#33B5E5"spacing: 4//清楚按钮Button {text: "Clear"onClicked: {canvas.clear()}}//绑定点击更换颜色Repeater {model: ["#33B5E5", "#99CC00", "#FFBB33", "#FF4444"]Rectangle {id: colorSquare;width: 48; height: 48color: modelDatasignal clickedproperty bool active: falseborder.color: active? "#666666" : "#f0f0f0"border.width: 2MouseArea {id: area1anchors.fill :parentonClicked: {colorTools.paintColor = color}}}}}//画框Rectangle{anchors.fill: canvasborder.color: "#666"border.width: 4;}Canvas {id: canvasanchors {left: parent.leftright: parent.righttop: colorTools.bottombottom: parent.bottommargins: 8}property real lastXproperty real lastYproperty color color: colorTools.paintColor  //继承颜色//清除函数function clear() {var ctx = getContext('2d')ctx.reset();canvas.requestPaint();}onPaint: {var ctx = getContext('2d')ctx.lineWidth = 1.5ctx.strokeStyle = canvas.colorctx.beginPath()ctx.moveTo(lastX, lastY)lastX = area.mouseXlastY = area.mouseYctx.lineTo(lastX, lastY)ctx.stroke()}MouseArea {id: areaanchors.fill: parentonPressed: {canvas.lastX = mouseXcanvas.lastY = mouseY}onPositionChanged: {canvas.requestPaint()}}}
}

3.3 简单鼠标绘制(转【参2】)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12Window{id: rootwidth: 640height: 480visible: true//鼠标点击坐标位置property real startX     //储存鼠标开始时的坐标property real startYproperty real stopX      //储存鼠标结束时的坐标property real stopYproperty bool isMouseMoveEnable: false//在root上画一个方框Rectangle{anchors.fill: canvasborder.color: "#666"border.width: 4;}//创建一个画布Canvas{id:canvas;anchors.fill:parentonPaint: {var ctx = getContext("2d")ctx.lineWidtn = 3ctx.strokeStyle = "blue";//开始绘制ctx.beginPath()ctx.moveTo(startX,startY)//纪录鼠标开始的位置的坐标点startX = area.mouseX;startY = area.mouseY;ctx.lineTo(startX,startY)ctx.stroke()}}MouseArea{id: areaanchors.fill: parent//当鼠标按下时调用本函数onPressed: {startX = area.mouseX;startY = area.mouseY;isMouseMoveEnable = true}//当鼠标释放时调用本函数onReleased: {isMouseMoveEnable = falsecanvas.requestPaint()      //当鼠标released时,调用绘制paint}//当鼠标press位置改变,调用本函数onPositionChanged: {if (isMouseMoveEnable){canvas.requestPaint()   //绘制函数}}}
}

3.4 绘制长方形(转【参2】)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12Window{id: rootwidth: 640height: 480visible: true//鼠标点击坐标位置property real startX     //储存鼠标开始时的坐标property real startYproperty real stopX      //储存鼠标结束时的坐标property real stopYproperty bool isMouseMoveEnable: false //是否允许鼠标移动绘制事件//在root上画一个方框Rectangle{anchors.fill: canvasborder.color: "#666"border.width: 4;}//创建一个画布Canvas{id:canvas;anchors.fill:parentonPaint: {var ctx = getContext("2d")ctx.lineWidtn = 3ctx.strokeStyle = "blue";if (!isMouseMoveEnable) {ctx.clearRect(0,0,width,height) //清空所画图形return;}if (isMouseMoveEnable){ctx.clearRect(0,0,width,height)}//开始绘制ctx.beginPath()ctx.moveTo(startX,startY)//记录移动终点stopX = area.mouseXstopY = area.mouseY//绘制长方形ctx.strokeRect(startX,startY,stopX-startX,stopY-startY)ctx.stroke()}}MouseArea{id:area;anchors.fill: parent;//当鼠标按下时调用本函数onPressed: {startX = mouse.x;startY = mouse.y;isMouseMoveEnable = true}//当鼠标press位置改变,调用本函数onPositionChanged: {if (isMouseMoveEnable){canvas.requestPaint()   //绘制函数}}}
}

参考

1、Qt–Canvas QML Type
2、Qt-quick(qml) Canvas用法及鼠标绘制图形
3、鼠标画图
4、QML画图-Canvas画基本图形
5、canvas arc()方法详解
6、Qt:在QML中自定义贝塞尔动画曲线
7、HTML canvas fillText() 方法
8、HTML canvas createLinearGradient() 方法
9、用qml画坐标
10、QML之CANVAS实现标尺(刻度尺)方案
11、在QML中使用Canvas
12、QML 画布Canvas–2D绘图(上)
13、提高canvas性能技巧
14、bezier curve or quadratic curve

Qt--canvas画图(5.12)相关推荐

  1. 今天的码农女孩做了关于svg画图和canvas画图 2022/1/18

    svg和canvas画图 svg和canvas区别: svg:不依赖分辨率,不能嵌入图片和文字,不能通过事件操作,但是可以通过css执行动画,矢量图形,放大缩小不失真,渲染能力强,适合做图标,地图,动 ...

  2. Canvas画图之烟花爆炸demo

    效果展示 canvas画图简单的烟花爆炸效果图如下: 思路分析 1.创建canvas元素 2.创建小圆圈对象,因为圆圈都是一组属性相同的数据,所以将实例化的圆圈对象都存储在数组中,方便后边圆圈消失使用 ...

  3. 微信小程序之canvas画图

    开题 前几天接到个需求,长按图片保存到相册,该图片上有用户头像和昵称以及对应的二维码:那这就不能直接当作图片来操作了,要先把整体图片画出来:我当时用的是canvas.效果图如下: canvas dra ...

  4. HTML5 canvas画图

    HTML5 canvas画图 HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript). 不过,<canvas> 元素本身并没有绘制能力(它仅仅 ...

  5. Qt最新版5.12在Windows环境静态编译安装和部署的完整过程(VS2017)

    文章目录 为什么要静态编译 1.源码下载 2. 编译工具下载 ActivePerl Python Ruby 编译环境选择 3.编译 1.修改源码里的qtbase\mkspecs\common\msvc ...

  6. html5 canvas 画图移动端出现锯齿毛边的解决方法

    html5 canvas 画图移动端出现锯齿毛边的解决方法 参考文章: (1)html5 canvas 画图移动端出现锯齿毛边的解决方法 (2)https://www.cnblogs.com/dear ...

  7. 小程序---canvas画图,生成分享图片,画图文字换行

    小程序目前只支持转发,不支持分享朋友圈,为了能实现分享,很多线上小程序通过生成分享图片,保存到相册来给用户增加分享的可能. 具体思路及简要代码如下: 一:canvas画图drawCanvas:func ...

  8. linux pyside2 安装包,Qt for Python 5.12发布下载,附PySide2和Shiboken2介绍

    Qt for Python 5.12(全称 Qt for Python for Qt 5.12) 发布下载了,这是一个没有 LTS 支持的版本,在终端中执行 pip install PySide2 命 ...

  9. 对QT学习之路12-14的源代码补充与修正

    QT学习之路12-14的源代码有些不完整,为了更好的让大家学习,本人做了一点修正与补充,谢谢.源代码如下: 头文件: #ifndef MAINWINDOW_H #define MAINWINDOW_H ...

  10. 解决canvas画图模糊的问题

    canvas 画图经常发现他是模糊的.解决这个问题主要从两个方面下手. 改变canvas渲染的像素 情况:画1像素的线条看起来模糊不清,好像更宽的样子. 解决方案 var ctx = canvas.g ...

最新文章

  1. ffmpeg端口被占用
  2. qt串口采用队列_基于STM32的RGB调色器——STM32程序和Qt上位机全开源
  3. 小姐姐教我的 sklearn 逻辑回归
  4. HTML5+NodeJs实现WebSocket即时通讯
  5. 不懂电容原理?那是你没看到这些动图
  6. android弹窗自动消失,Android点击popupwindow以外区域 popupwindow自动消失(转载)
  7. CDU集训代码:基础算法和数据结构2
  8. Maven:解决jar包冲突和企业开发常用编写
  9. 【操作系统】第3章 进程管理与调度
  10. html5编写商城页面,HTML5+CSS3前端入门教程---从0开始通过一个商城实例手把手教你学习PC端和移动端页面开发第2章HTML基础知识...
  11. wdcp安装中的小知识
  12. saas商业级的小程序商城(已开源)
  13. 计算机教案制作电子表格,制作电子表格教案
  14. 20.数据集成、数据整合、数据融合
  15. 101个著名的管理学及心理学效应
  16. PHP的apcu是什么,opcache又是什么?
  17. css3选择器详细探索
  18. 循环神经网络LSTM论文解读
  19. 《一本书读懂24种互联网思维》---- 读书笔记
  20. 什么是嵌入式开发?初学者必看嵌入式学习课程

热门文章

  1. APP强制更新和非强制更新测试要点
  2. 快速了解常用的对称加密算法,再也不用担心面试官的刨根问底
  3. RxJava(三)-合并操作符
  4. 资产管理5大优势,最后一个绝了
  5. 原 大数据入门学习,你要掌握这些技能
  6. 【java】小鱼的游泳时间
  7. HTML:三种方法用JavaScript修改CSS样式
  8. 利用Java8优化if else
  9. 威胁分析矩阵(转载)
  10. Tomcat面试题笔记