在我们写QML应用时,我们如何来优化我们的应用呢?在Ubuntu平台的API中,有一个API叫做"PerformanceOverlay".就像它的名字所说的,它是一个overlay.它可以用来显示一个应用启动或运行时所需要的时间及CPU的使用情况.我们下面来用一个小的应用来测试一下:
import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.PerformanceMetrics 0.1
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "performanceoverlay.liu-xiao-guo"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false
width: units.gu(60)
height: units.gu(85)
PerformanceOverlay {
active: true
}
Page {
title: i18n.tr("PerformanceOverlay")
Column {
spacing: units.gu(1)
anchors {
margins: units.gu(2)
fill: parent
}
Label {
id: label
objectName: "label"
text: i18n.tr("Hello..")
}
Button {
objectName: "button"
width: parent.width
text: i18n.tr("Tap me!")
onClicked: {
label.text = i18n.tr("..world!")
}
}
}
}
}
在上面的图中,我们可以看出CPU的使用率,同时,当我们点击"Tap me"按钮时所显示需要的时间.如果显示为红色,它意味着我们需要做以下事情来优化我们的应用.
所有的源码在https://github.com/liu-xiao-guo/performanceoverlay