这是qml文件。 “main.qml”
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
ApplicationWindow{
visible:true
width:640
height:480
id:window
title:asTr("editor")
signal show(string text)
TextArea{
text:"hello"
onTextChanged:show(text);
}
}
这是python代码。 “main.py”
import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
def show(text):
print(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('main.qml')
win = engine.rootObjects()[0]
win.show()
sys.exit(app.exec_())
我想将main.qml中的信号“show”连接到main.py中的插槽“show”。我怎么能这样做?
答案 0 :(得分:2)
首先也是最重要的:不要拨打你的信号show
,show已经是QWindow的一个插槽。如果将名称更改为其他名称,则只需在python代码中连接qml中定义的信号:
QML:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
ApplicationWindow{
visible:true
width:640
height:480
id:window
title: "editor"
signal textUpdated(string text)
TextArea{
text:"hello"
onTextChanged: textUpdated(text);
}
}
蟒:
import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
def show(text):
print(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('main.qml')
win = engine.rootObjects()[0]
win.textUpdated.connect(show)
win.show()
sys.exit(app.exec_())