【发布时间】:2021-10-13 11:02:25
【问题描述】:
我正在为我的 QML 应用程序创建一个“虚线纸”组件。本质上,这些组件由用户可以放大的虚线背景组成。为了创建它,我使用了以下代码:
DottedPaper.qml
import QtQuick 2.15
Rectangle{
id: page
property int spacing: 50
// this property changes the spacing between the dots
property real contentScale: 1.0
Repeater{
id: rowRepeater
model: 200
Repeater{
id: row
property int rowIndex: index
model: 200
Rectangle{
id: circle
color: "red"
width: 4; height: 4
x: index * spacing * page.contentScale + width/2;
y: rowIndex * spacing * page.contentScale+ height/2
radius: width/2
}
}
}
}
然后在我的主文件中,我有以下内容:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Flickable{
clip: true // doesn't improve anything
anchors.fill: parent
contentWidth: paper.width
contentHeight: paper.height
DottedPaper{
id: paper
width: 10000; height: 10000;
clip: true
}
}
// the slider changes the content scale of the dotted paper
Slider{
from: 1
to: 3
onValueChanged: paper.contentScale = value
}
}
这段代码的问题是它很慢。检查应用程序后,内容不会以 60 fps 的速度呈现,但我的桌面上的速度约为 45 - 50 fps。我怀疑问题在于 QML 正在重新评估背景中每个圆圈的 contentScale 属性绑定,即使圆圈在屏幕上不可见。防止这种情况的最佳方法是什么?
【问题讨论】:
-
我建议在一个Canvas 上画出所有的点。
标签: performance qt qml qtquick2