python - 如何将文本文件(或字符串发送到 QML 中的 textArea)从 python 到 QML 应用程序?

标签 python pyqt qml pyqt5

我想将字符串内容(多行)从 Python 发送到文本区域中的 QML 应用程序。那么我该怎么做。

测试.py

def send_file(file_content)
    pass // send file to QML text area

test.qml

Window {
    id: mainWindow
    property alias text: textArea.text

    function read_file(){
        mainWindow.text = send_file(file_content) //Strings from python
    }

    TextArea{
         id: textArea
    }
}

最佳答案

如果你想从 Python 发送信息到 QML 你必须创建一个继承自 QObject 的类并且有一个 q -property 存储该值,然后使用 setContextProperty() 将该类的对象导出到 QML,并在 QML 上一边它执行绑定(bind),如继续所示:

主.py

import sys

from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine


class Helper(QObject):
    textChanged = pyqtSignal()

    def __init__(self, parent=None):
        QObject.__init__(self, parent)
        self._text = ""

    @pyqtProperty(str, notify=textChanged)
    def text(self):
        return self._text

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

    def send_file(self, file_content):
        self.text = file_content

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)

    engine = QQmlApplicationEngine()
    helper = Helper()
    engine.rootContext().setContextProperty("helper", helper)
    engine.load(QUrl.fromLocalFile('main.qml'))
    if not engine.rootObjects():
        sys.exit(-1)

    helper.send_file("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ornare magna felis. Nulla justo ipsum, finibus eu nibh quis, iaculis malesuada lorem. Phasellus et lacus malesuada, aliquam enim condimentum, efficitur sapien. Sed ultricies egestas massa, nec sodales neque mattis et. Praesent euismod pretium hendrerit. Maecenas non porttitor velit, non scelerisque quam. Phasellus at diam vel enim venenatis vulputate sed a nisl. Sed erat nunc, maximus varius justo vitae, vehicula porttitor enim. Maecenas vitae sem odio. Nunc interdum sapien vitae magna tempus, nec laoreet elit placerat. Nullam cursus metus facilisis pulvinar auctor.")
    sys.exit(app.exec_())

ma​​in.qml

import QtQuick 2.10
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: mainWindow
    visible: true

    TextArea{
         id: textArea
         anchors.fill: parent
         text: helper.text
    }
}

关于python - 如何将文本文件(或字符串发送到 QML 中的 textArea)从 python 到 QML 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50782747/

相关文章:

python - 无法让 Nose 遵守我在测试中设置的属性

python - eclipse (pydev) : Is it possible to assign a shortcut to send selection to the python console?

windows - Windows下使用PyQt的Webkit无法通过xhr获取远程资源

qt - 拖放项目时滚动项目

qt - Windows 上 Bash 上带有 qmlscene 的简单 QML 会导致段错误

Python SqlAlchemy 查询 Unicode 值在 'filter_by' 中的 session 返回 null

python - Beautiful Soup 查找标签是否存在

pyqt - 画线: retrieve the covered pixels

python - Pyqt - 从模型中获取 CSV

qt - QML:调整 ListView 页脚组件的大小以填充剩余空间