我如何在pyqt5中绑定qtquick按钮控件 [英] how i can bind qtquick button control in pyqt5
本文介绍了我如何在pyqt5中绑定qtquick按钮控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我如何在 pyqt5 中绑定 qtquick 按钮,我有两个文件,一个用于 qml,一个用于 python,现在我希望按钮应该做一些事情,例如只打印一些东西,以便我现在可以绑定,
Hello how i can bind a qtquick button in pyqt5, i have two file one for qml and one for python now i want that the button should do something for example just print something so how i can bind now,
Button.py
import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
ctx = engine.rootContext()
ctx.setContextProperty("main", engine)
engine.load('Button.qml')
win = engine.rootObjects()[0]
win.show()
sys.exit(app.exec_())
Button.qml
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2
import QtQuick.Dialogs 1.1
ApplicationWindow {
title: qsTr("Test Invoke")
width: 200
height: 100
Button{
y : 70
text : "About"
onClicked: {
print('Hello')
}
}
}
推荐答案
这是一个使用 QML QtQuick(信号和槽)的小例子.程序文本中的一些解释.试试看:
Here is a small example of working with QML QtQuick (Signals and slots). Some explanations in the text of the program. Try it:
按钮.py
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
class Main(QObject):
def __init__(self):
QObject.__init__(self)
# signal sending string
# necessarily give the name of the argument through arguments=['text1']
# otherwise it will not be possible to pick it up in QML
textResult = pyqtSignal(str, arguments=['text1'])
@pyqtSlot(str)
def text1(self, arg1):
# do something with the text and emit a signal
arg1 = arg1.upper()
self.textResult.emit(arg1)
if __name__ == "__main__":
import sys
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
main = Main()
engine.rootContext().setContextProperty("main", main)
engine.load("Button.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())
按钮.qml
import QtQuick 2.7
//import QtQuick.Window 2.1
import QtQuick.Controls 1.4
//import QtQuick.Dialogs 1.1
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
title: qsTr("Test Invoke")
width: 400
height: 100
color: "whitesmoke"
GridLayout {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.margins: 9
columns: 4
rows: 2
rowSpacing: 10
columnSpacing: 10
Text {
text: qsTr("Enter text")
}
// Text input box
TextField {
id: firstString
}
Button {
height: 40
Layout.fillWidth: true
text: qsTr("Send text for processing")
Layout.columnSpan: 2
onClicked: {
// call the slot to process the text
main.text1(firstString.text)
}
}
Text {
text: qsTr("Result")
}
// Here we see the result of text processing
Text {
id: textResult
}
}
// Here we take the result of text processing
Connections {
target: main
// Signal Handler
onTextResult: {
// text1 - was given through arguments=['text1']
textResult.text = text1
}
}
}
这篇关于我如何在pyqt5中绑定qtquick按钮控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文