如何将文本文件(或字符串发送到QML中的textArea)从python发送到QML应用程序?
我想将字符串内容(多行)从Python发送到文本区域中的QML应用程序。那么我该怎么做呢 test.py如何将文本文件(或字符串发送到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
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怎么搞的?