• 4085阅读
  • 4回复

Canvas 画线 改变Canvas大小后不能成功画到变大后的Canvas 里 [复制链接]

上一主题 下一主题
离线alicker
 

只看楼主 倒序阅读 楼主  发表于: 2015-01-17
如题,画了一条直线,拉伸窗体时,直线显示的区域没有变,还是初始大小,不知道为什么,求助
代码
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.getContext("2d");
            ctx.globalCompositeOperation="source-over";
            ctx.lineWith=1;
            console.log("width="+canvas.width+" height="+canvas.height);
            console.log(canvas.canvasSize)
            console.log(canvas.canvasWindow);
            console.log(canvas.available);
            console.log(canvas.tileSize);
            console.log(canvas.contextType);
            console.log(canvas.renderStrategy);
            console.log(canvas.renderTarget+"\n");
            drawLine(0,0,canvas.width,canvas.height,ctx);
        }
    }
}


离线彩阳

只看该作者 1楼 发表于: 2015-01-17
想办法让Rectangle的大小绑定至窗口的大小。
另外,尽量使用ApplicationWindow来作为顶层窗口使用吧。
上海Qt开发联盟,热忱地欢迎你的加入!
离线alicker

只看该作者 2楼 发表于: 2015-01-18

恩  改了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);
        }
    }
}
离线pangpangpang

只看该作者 3楼 发表于: 2015-03-30
你在调整窗口大小后, 要调用重绘.
离线tommychenye

只看该作者 4楼 发表于: 2015-05-18
当窗口的大小变化时需要 调用 canvas.requestPaint()
快速回复
限100 字节
 
上一个 下一个