首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QML项对CPU性能的影响

QML项对CPU性能的影响
EN

Stack Overflow用户
提问于 2022-04-22 18:33:13
回答 1查看 60关注 0票数 0

我怎么能关闭项目(主要是文本,显示传感器读数),所以它不会消耗CPU的力量?能见度=假不起作用,对我是100%正确的。

我有很多传感器连接到这个程序上。在python方面,它们非常轻量级--它们消耗了大约2%的CPU (windows任务管理器读数)。但是当我运行GUI时,整个消耗约为28%,当我关闭文本的可见性时,它只会下降约几个百分点--也许下降到22%。

下面我给出一个小例子,这样您就可以检查它在您的机器上是如何工作的。在这个例子中,当它运行可见时,它消耗了CPU能量的5-6%,当我关闭可见性时,它稍微下降到4-6%,当我最小化窗口时,它会下降到1%以下。通过关闭项目的可见性,我希望达到最小化窗口的结果,这样GUI将完全避免呈现这些文本,我可以做其他的事情。

值得补充的是,我希望能够关闭单个读数(通过复选框),而不是整个网格,因此在我的情况下,使用Loader可能无法工作。在文本中,我也使用上标、下标、程度符号等,所以有时是RichText,有时不是。我不确定这里能不能做些复读机?

main.py:

代码语言:javascript
复制
import os
import time
from pathlib import Path
import sys
import random
import threading

from PySide2.QtGui import QGuiApplication, QIcon
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot, Signal, Property


class TestPythonBackend(QObject):
    def __init__(self):
        QObject.__init__(self)
        self.random_float = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        thr1 = threading.Thread(target=self.func, daemon=True)
        thr1.start()

    def func(self):
        while True:
            self.random_float = [random.random() * 1, random.random() * 2, random.random() * 3, random.random() * 4, random.random() * 5, random.random() * 6, random.random() * 7, random.random() * 8, random.random() * 9, random.random() * 10, random.random() * 11, random.random() * 12]
            self.random_float_property_changed.emit()
            time.sleep(0.02)

    random_float_property_changed = Signal()

    @Property(list, notify=random_float_property_changed)
    def random_float_property(self):
        return self.random_float


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    test_python_backend = TestPythonBackend()
    engine = QQmlApplicationEngine()
    engine.rootContext().setContextProperty("test_python_backend", test_python_backend)
    engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

main.qml:

代码语言:javascript
复制
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.11

Window {
    id: window
    width: 1280
    height: 800
    visible: true
    title: qsTr("Hello World")

    property var randomFloat: test_python_backend.random_float_property


    CheckBox {
        id: checkBox
        text: qsTr("Visibility")
        checked: true
        anchors.verticalCenter: parent.verticalCenter
        anchors.left: grid1.right
        anchors.right: parent.right
        anchors.rightMargin: 200
        anchors.leftMargin: 200
        onCheckStateChanged: checked ? grid1.visible = true : grid1.visible = false
    }


    GridLayout{
        id: grid1
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        columns: 3
        anchors.leftMargin: 50
        anchors.bottomMargin: 50
        anchors.topMargin: 50
        width: parent.width/2

        Text {
            id: text1
            text: qsTr("Data 1: ") + randomFloat[0].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text2
            text: qsTr("Data 2: ") + randomFloat[1].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text3
            text: qsTr("Data 3: ") + randomFloat[2].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text4
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text5
            text: qsTr("Data 5: ") + randomFloat[4].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text6
            text: qsTr("Data 6: ") + randomFloat[5].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text7
            text: qsTr("Data 7: ") + randomFloat[6].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text8
            text: qsTr("Data 8: ") + randomFloat[7].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text9
            text: qsTr("Data 9: ") + randomFloat[8].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text10
            text: qsTr("Data 1: ") + randomFloat[0].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text11
            text: qsTr("Data 2: ") + randomFloat[1].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text12
            text: qsTr("Data 3: ") + randomFloat[2].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text13
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text14
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text15
            text: qsTr("Data 5: ") + randomFloat[4].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text16
            text: qsTr("Data 6: ") + randomFloat[5].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text17
            text: qsTr("Data 7: ") + randomFloat[6].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text18
            text: qsTr("Data 8: ") + randomFloat[7].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text19
            text: qsTr("Data 9: ") + randomFloat[8].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text20
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text21
            text: qsTr("Data 1: ") + randomFloat[0].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text22
            text: qsTr("Data 2: ") + randomFloat[1].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text23
            text: qsTr("Data 3: ") + randomFloat[2].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text24
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text25
            text: qsTr("Data 5: ") + randomFloat[4].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text26
            text: qsTr("Data 6: ") + randomFloat[5].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: text27
            text: qsTr("Data 7: ") + randomFloat[6].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext1
            text: qsTr("Data 1: ") + randomFloat[0].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext2
            text: qsTr("Data 2: ") + randomFloat[1].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext3
            text: qsTr("Data 3: ") + randomFloat[2].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext4
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext5
            text: qsTr("Data 5: ") + randomFloat[4].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext6
            text: qsTr("Data 6: ") + randomFloat[5].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext7
            text: qsTr("Data 7: ") + randomFloat[6].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext8
            text: qsTr("Data 8: ") + randomFloat[7].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext9
            text: qsTr("Data 9: ") + randomFloat[8].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext10
            text: qsTr("Data 1: ") + randomFloat[0].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext11
            text: qsTr("Data 2: ") + randomFloat[1].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext12
            text: qsTr("Data 3: ") + randomFloat[2].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext13
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext14
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext15
            text: qsTr("Data 5: ") + randomFloat[4].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext16
            text: qsTr("Data 6: ") + randomFloat[5].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext17
            text: qsTr("Data 7: ") + randomFloat[6].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext18
            text: qsTr("Data 8: ") + randomFloat[7].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext19
            text: qsTr("Data 9: ") + randomFloat[8].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext20
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext21
            text: qsTr("Data 1: ") + randomFloat[0].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext22
            text: qsTr("Data 2: ") + randomFloat[1].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext23
            text: qsTr("Data 3: ") + randomFloat[2].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext24
            text: qsTr("Data 4: ") + randomFloat[3].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext25
            text: qsTr("Data 5: ") + randomFloat[4].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext26
            text: qsTr("Data 6: ") + randomFloat[5].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Text {
            id: ttext27
            text: qsTr("Data 7: ") + randomFloat[6].toFixed(4)
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }

        Image {
            id: img1
            sourceSize.width: 100
            sourceSize.height: 100
            source: "Qt_logo_2016.svg"
            Layout.fillWidth: true
            fillMode: Image.PreserveAspectFit
            rotation: randomFloat[9]
        }

        Image {
            id: img2
            sourceSize.width: 100
            sourceSize.height: 100
            source: "Qt_logo_2016.svg"
            Layout.fillWidth: true
            fillMode: Image.PreserveAspectFit
            rotation: randomFloat[10]
        }

        Image {
            id: img3
            sourceSize.width: 100
            sourceSize.height: 100
            source: "Qt_logo_2016.svg"
            Layout.fillWidth: true
            fillMode: Image.PreserveAspectFit
            rotation: randomFloat[11]
        }
    }
}

Qt图像:https://en.wikipedia.org/wiki/Qt_(software)#/media/File:Qt_logo_2016.svg

我的规格:

  • 笔记本电脑,Ryzen 5 4600H,GTX 1650 10,16 10内存和SSD驱动
  • Windows 10
  • Python 3.9.4
  • PySide2 5.15.2

F 212

EN

回答 1

Stack Overflow用户

发布于 2022-04-22 19:11:12

尝试这种方法来消除大量的绑定活动:

代码语言:javascript
复制
[...]
    property var qsTrCache: []; 

    Component.onCompleted: { 
        qsTrCache["Data 1: "] = qsTr("Data 1: ");
        [...]
    }

    onRandomFloatChanged: {
        // You could also use two different Texts instead of precomputing qsTr() calls.
        text1.text = qsTrCache["Data 1: "] + randomFloat[0].toFixed(4);
        text2.text = qsTrCache["Data 2: "] + randomFloat[1].toFixed(4);
        // Etc.
    }
[...]
        Text {
            id: text1
            font.pixelSize: 12
            Layout.fillWidth: true
            textFormat: Text.RichText
        }
[...]

谢谢@JarMan评论re:预计算。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71973226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档