[Interest] Qml Canvas is too slow

Christoph Keller theoriginalgri at gmail.com
Tue Nov 5 07:53:03 CET 2019


If this is just about drawing squares, you can also use a *GridView* or 
*Repeater* and repeat a delegate of *Rectangle*s. If that's not fast 
enough, Jérôme Godbout approach also sounds good.

Alternatively, you should prefer *QtQuick.Shapes* to *Canvas*: 
https://doc.qt.io/qt-5/qml-qtquick-shapes-shape.html

Regards,
Christoph

On 31.10.19 17:42, Alexander Dyagilev wrote:
>
> Hello,
>
> The following code is too slow (paint operation takes few seconds):
>
> Canvas{
>
> id:map
> width:columnsCount*rectangleSize
> height:rowsCount*rectangleSize
> anchors.horizontalCenter:alignCenter?parent.horizontalCenter:undefined
> anchors.left:alignCenter?undefined:parent.left
> anchors.bottom:parent.bottom
> propertyintoffset:1
> onPaint:drawMap()
> functiondrawMap(){
> if(columnsCount===0||rowsCount===0){
> return;
> }
> varmap=downloadProgressMap.map();
> varctx=getContext("2d");
> for(vari=0;i<map.length;i++){
> varx=(i%columnsCount)*rectangleSize;
> vary=(Math.floor(i/columnsCount))*rectangleSize;
> if(map[i]){
> drawFillRect(ctx,x,y);
> }else{
> drawClearRect(ctx,x,y);
> }
> }
> }
> functiondrawFillRect(ctx,x,y){
> ctx.fillStyle=appWindow.theme.progressMapFillBorder
> ctx.fillRect(x+offset,y+offset,rectangleSize-offset*2,rectangleSize-offset*2);
> ctx.fillStyle=appWindow.theme.progressMapFillBackground
> ctx.fillRect(x+offset+1,y+offset+1,rectangleSize-(offset+1)*2,rectangleSize-(offset+1)*2);
> }
> functiondrawClearRect(ctx,x,y){
> ctx.fillStyle=appWindow.theme.progressMapClearBorder
> ctx.fillRect(x+offset,y+offset,rectangleSize-offset*2,rectangleSize-offset*2);
> ctx.fillStyle=appWindow.theme.background
> ctx.fillRect(x+offset+1,y+offset+1,rectangleSize-(offset+1)*2,rectangleSize-(offset+1)*2);
> }
> }
>
> Can anything be done to improve its speed, or should we use c++ instead?
>
> It paints the following:
>
>
>
> Map size: 2323 elements.
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191105/45799e1d/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: laaeocmjenhcnjkg.png
Type: image/png
Size: 2589 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191105/45799e1d/attachment-0001.png>


More information about the Interest mailing list