Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文本文件(或字符串发送到QML中的textArea)从python发送到QML应用程序?_Python_Pyqt_Qml_Pyqt5 - Fatal编程技术网

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

如何将文本文件(或字符串发送到QML中的textArea)从python发送到QML应用程序?,python,pyqt,qml,pyqt5,Python,Pyqt,Qml,Pyqt5,我想将字符串内容(多行)从Python发送到文本区域中的QML应用程序。那么我该怎么做呢 test.py def send_file(file_content) pass // send file to QML text area import sys from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty, QUrl from PyQt5.QtGui import QGuiApplication from PyQt5.Q

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

test.py

def send_file(file_content)
    pass // send file to QML text area
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_())
测试.qml

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

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

    TextArea{
         id: textArea
    }
}
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
,则必须创建一个从
QObject
继承的类,并使用
q-property
存储该值,然后使用
setContextProperty()
将该类的对象导出到
QML
,在
QML
侧,它执行绑定,如图所示:

main.py

def send_file(file_content)
    pass // send file to QML text area
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_())
main.qml

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

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

    TextArea{
         id: textArea
    }
}
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
    }
}

多谢各位much@RohanMukundMiraje怎么搞的?