QT QML加载程序异步文本速度慢



我有一个QML场景,它以60fps的速度平稳运行。

我正在使用Loader异步加载一个包含带有表情符号的文本的组件。QT在主线程中创建Text对象,qml探查器显示它需要183ms,我正在跳过帧,因为创建项目需要16ms以上。下面是一个计时器复制相同的例子

Component {
id: textTest
Text {
anchors.centerIn: parent
text: "Sample Test🤚"
font.pointSize: 32
color: "blue"
}
}
Timer {
interval: 8000
running: true
repeat: false
onTriggered: tester1.sourceComponent = textTest
}
Loader {
anchors.fill: parent
id: tester1
asynchronous: true
visible: true
}

我可以在另一个线程中创建文本项吗?这样我就可以避免掉帧了。

我可以将QQuickText子类化并在另一个线程中进行创建吗?我可以用我的自定义QQuickItems来做这件事,但不确定如何用文本来做

我注意到,用大字体渲染表情符号看起来确实很慢。感觉好像字体加载程序正在以该大小光栅化整个字体。为了避免这种情况,我们可以考虑找到一个与我们正在渲染的内容等效的SVG。此外,对于组件的动态创建,我通常更喜欢使用带有中继器的ListModel。这样做的优点是通过在ListModel中添加/删除项来简化记录的动态创建和销毁。

import QtQuick
import QtQuick.Controls
import QtQuick3D
Page {
id: thisPage
Repeater {
model: listModel
TestText {
x: px
y: py
}
}
ListModel {
id: listModel
}
Timer {
id: timer
repeat: false
interval: 50
onTriggered: {
let px = Math.floor(Math.random() * thisPage.width);
let py = Math.floor(Math.random() * thisPage.height);
listModel.append({px,py});
if (listModel.count < 20) Qt.callLater(start)
}
}
footer: Button {
text: qsTr("Start")
onClicked: {
listModel.clear();
timer.restart();
}
}
}
//TestText.qml
import QtQuick
import QtQuick.Controls
Frame {
background: Rectangle {
border.color: "#e0e0e0"
}
Row {
Text {
text: "Sample Test"
font.pointSize: 32
color: "blue"
}
Image {
width: 48
height: 48
source: "hand.svg"
sourceSize: Qt.size(width, height)
}
}
}
//hand.svg
<?xml version="1.0" encoding="iso-8859-1"?>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 494.147 494.147" style="enable-background:new 0 0 494.147 494.147;" xml:space="preserve">
<path fill="orange" d="M455.84,172.391l-84.02-84.02c-13.278-13.278-34.805-13.278-48.083,0c-13.278,13.278-13.278,34.805,0,48.083l67.882,67.882
l-7.071,7.071L197.847,24.707c-13.278-13.278-34.805-13.278-48.083,0c-13.278,13.278-13.278,34.805,0,48.083l123.062,123.062
l-7.071,7.071L119.257,56.425c-13.278-13.278-34.805-13.278-48.083,0c-13.278,13.278-13.278,34.805,0,48.083l146.497,146.497
l-7.071,7.071L87.538,135.015c-13.278-13.278-34.805-13.278-48.083,0s-13.278,34.806,0,48.083L162.516,306.16l-7.071,7.071
l-97.404-97.404c-13.278-13.278-34.805-13.278-48.083,0c-13.278,13.278-13.278,34.805,0,48.083l177.181,177.181
c51.077,51.077,134.185,51.077,185.262,0l83.439-83.439C506.917,306.576,506.916,223.468,455.84,172.391z"/>
</svg>

你可以在线试用!

最新更新