首页| 论坛| 消息

标题:Canvas 画线 改变Canvas大小后不能成功画到变大后的Canvas 里
作者:alicker
日期:2015-01-17 09:03
内容:

如题,画了一条直线,拉伸窗体时,直线显示的区域没有变,还是初始大小,不知道为什么,求助
代码
import QtQuick 2.2
Rectangle
{
id: chart
width: 600
height: 400
color: "transparent"
Canvas
{
id: canvas
anchors.fill:parent
function drawLine(x1,y1,x2,y2,ctx)
{
ctx.save();
ctx.fillStyle="#ffffffff";
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.strokeStyle ="#ffff0000";
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
onPaint:
{
var ctx=canvas.getC ..


#1 [彩阳 01-17 10:37]
想办法让Rectangle的大小绑定至窗口的大小。
另外,尽量使用ApplicationWindow来作为顶层窗口使用吧。
#2 [alicker 01-18 12:21]
恩改了ApplicationWindow 还是没办法画出初始窗体大小的范围
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.1
ApplicatonWindow
{
visible:true
width:400
height:600
Canvas
{
id:canvas;
anchors.fill:parent;
function drawLine(x1,y1,x2,y2,ctx)
{
ctx.save();
ctx.strokeStyle="red";
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.stroke();
ctx.restore();
}
onPaint:
{
var ctx=canvas.getContext("2d");
console.log("width="+canvas.width+" height="+canvas.height);
drawLine(0,0,canvas.width,canvas.height,ctx);
..
#3 [pangpangpang 03-30 15:43]
你在调整窗口大小后, 要调用重绘.
#4 [tommychenye 05-18 15:54]
当窗口的大小变化时需要 调用 canvas.requestPaint()

回复 发表
主题 版块