.qml 上的地址文本元素

Address text element on .qml

我想更改图形用户界面中的文本。我可以解决我找到解决方案的 ListView 元素。

我也想任意处理文本元素,例如替换文本。

此外,我无法处理另一个具有不同内容的 ListView 元素。

这是我的小程序。请原谅我的英语 main.py

import sys
import vensoft_main
from time import sleep
import urllib.request
from pathlib import Path
from PySide6 import QtCore, QtWidgets, QtGui
from PySide6.QtQuick import QQuickView
from PySide6.QtCore import QStringListModel, QUrl
from PySide6.QtGui import QGuiApplication
from PySide6.QtWidgets import QApplication
import json
import sys
from typing import Text
import urllib.request
import json
import pandas as pd 
import random

if __name__ == '__main__':
    # get our data
    url = "file:///Users/joerg/Documents/python/Rohertrag/rohertrag_fenster/output_heute.json"
    response = urllib.request.urlopen(url)
    data = json.loads(response.read().decode('utf-8'))
    
    # Format and sort the data
    data_list = list(data.values())
    
    # Set up the application window
    app = QGuiApplication(sys.argv)
    view = QQuickView()
    view.setResizeMode(QQuickView.SizeRootObjectToView)
    view.update()

    # Expose the list to the Qml code
    my_model = QStringListModel()
    my_model.setStringList(data_list)
    view.setInitialProperties({"myModel": my_model}) 


    # Load the QML file
    qml_file = Path(__file__).parent / "view.qml"
    view.setSource(QUrl.fromLocalFile(qml_file.resolve()))
   
    # Show the window
    if view.status() == QQuickView.Error:
        sys.exit(-1)
    view.show()
    view.update()
   
    # execute and cleanup
    app.exec()
    del view

new.qml

import QtQuick
import QtQuick.Controls
Page {
    width: 640
    height: 480

    Rectangle {
        id: root

        anchors.fill: parent

        ListView {
            id: view_1

            anchors.fill: parent
            anchors.margins: 25
            anchors.bottomMargin: 230
            anchors.rightMargin: 375
            model: manager.model

            delegate: Text {
                anchors.leftMargin: 50
                font.pointSize: 15
                horizontalAlignment: Text.AlignHCenter
                text: display
            }

        }

        Text {
            id: text1

            x: 486
            y: 46
            width: 127
            height: 118
            text: manager.text
            font.pixelSize: 12
        }

        ListView {
            id: view_2
            anchors.fill: parent
            anchors.margins: 25
            anchors.leftMargin: 25
            anchors.topMargin: 238
            anchors.rightMargin: 375
            delegate: Text {
                text: display
                horizontalAlignment: Text.AlignHCenter
                anchors.leftMargin: 50
                font.pointSize: 15
            }
            anchors.bottomMargin: 17
            model: manager.model
        }

        Text {
            id: text2
            x: 479
            y: 272
            width: 127
            height: 118
            text: manager.text
            font.pixelSize: 12
        }

    }

    header: Label {
        color: "#15af15"
        text: qsTr("Wie ist den der Umsatz Heute?")
        font.pointSize: 17
        font.bold: true
        font.family: "Arial"
        renderType: Text.NativeRendering
        horizontalAlignment: Text.AlignHCenter
        padding: 10
    }

}

Less is more,你不需要放置不必要的导入,也不需要外部资源的数据,一个简单的列表就足够了。

如果您想从 python 操作 QML 视图,那么最好创建一个将数据作为属性的 QObject 并将其导出到 QML。

import sys
from pathlib import Path

from PySide6.QtCore import (
    Property,
    QDateTime,
    QObject,
    QStringListModel,
    QTimer,
    QUrl,
    Signal,
)
from PySide6.QtQuick import QQuickView
from PySide6.QtGui import QGuiApplication


class Manager(QObject):
    text_changed = Signal(name="textChanged")

    def __init__(self, parent=None):
        super().__init__(parent)
        self._model = QStringListModel()
        self._text = ""

    @Property(QObject, constant=True)
    def model(self):
        return self._model

    @Property(str, notify=text_changed)
    def text(self):
        return self._text

    @text.setter
    def text(self, text):
        if self.text == text:
            return
        self._text = text
        self.text_changed.emit()


def main():
    # get our data
    data_list = ["foo", "bar", "baz"]

    manager = Manager()

    # Set up the application window
    app = QGuiApplication(sys.argv)
    view = QQuickView()
    view.rootContext().setContextProperty("manager", manager)
    view.setResizeMode(QQuickView.SizeRootObjectToView)

    manager.model.setStringList(data_list)
    manager.text = "text"

    qml_file = Path(__file__).parent / "view.qml"
    view.setSource(QUrl.fromLocalFile(qml_file.resolve()))

    if view.status() == QQuickView.Error:
        sys.exit(-1)

    view.show()

    def handle_timeout():
        text = QDateTime.currentDateTime().toString()
        manager.text = text

    timer = QTimer(interval=1000, timeout=handle_timeout)
    timer.start()

    app.exec()
    del view


if __name__ == "__main__":
    main()
import QtQuick
import QtQuick.Controls

Page {
    width: 640
    height: 480

    Rectangle {
        id: root

        anchors.fill: parent

        ListView {
            id: view_1

            anchors.fill: parent
            anchors.margins: 25
            anchors.rightMargin: 375
            model: manager.model

            delegate: Text {
                anchors.leftMargin: 50
                font.pointSize: 15
                horizontalAlignment: Text.AlignHCenter
                text: display
            }

        }

        Text {
            id: text1

            x: 486
            y: 46
            width: 127
            height: 201
            text: manager.text
            font.pixelSize: 12
        }

    }

    header: Label {
        color: "#15af15"
        text: qsTr("Wie ist den der Umsatz Heute?")
        font.pointSize: 17
        font.bold: true
        font.family: "Arial"
        renderType: Text.NativeRendering
        horizontalAlignment: Text.AlignHCenter
        padding: 10
    }

}