使用PYQT 5从.QML文件获取单选按钮值
如何使用PYQT 5从.QML文件获取单选按钮值?
.QML文件如下:
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
width: 600
height: 500
title: "HelloApp"
RadioButton {
id: button1
text: "1"
objectName: "radio_button"
}
TextInput {
id: text_input
objectName: "Textinput"
x:300
y:300
width: 80
height: 20
}
Button {
id: button_execute
x: 158
y: 341
width: 211
height: 36
text: qsTr("Execute")
onClicked: {
con.on_execute()
}
}
}
用于读取此.QML文件的Python代码如下:
import sys
from os.path import abspath, dirname, join
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSlot
class Bridge(QObject):
@pyqtSlot()
def on_execute(self):
win = engine.rootObjects()[0]
# This will give me the value of text input
text_val = win.findChild(QObject, "Textinput").property("text")
# How to get the radio button value if it's checked or not checked?
app = QGuiApplication(sys.argv)
if __name__ == '__main__':
# Reading the qml file
engine = QQmlApplicationEngine()
context = engine.rootContext()
bridge = Bridge()
context.setContextProperty("con", bridge)
qmlFile = join(dirname(__file__), 'qml_test_1.qml')
engine.load(abspath(qmlFile))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
我如何知道是否检查了单个单选按钮?我尝试使用对象名称。但这不起作用。
How can I get the radio button value from a .qml file using PyQt 5?
The .qml file is as follows:
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
width: 600
height: 500
title: "HelloApp"
RadioButton {
id: button1
text: "1"
objectName: "radio_button"
}
TextInput {
id: text_input
objectName: "Textinput"
x:300
y:300
width: 80
height: 20
}
Button {
id: button_execute
x: 158
y: 341
width: 211
height: 36
text: qsTr("Execute")
onClicked: {
con.on_execute()
}
}
}
The Python code for reading this .qml file is as follows:
import sys
from os.path import abspath, dirname, join
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSlot
class Bridge(QObject):
@pyqtSlot()
def on_execute(self):
win = engine.rootObjects()[0]
# This will give me the value of text input
text_val = win.findChild(QObject, "Textinput").property("text")
# How to get the radio button value if it's checked or not checked?
app = QGuiApplication(sys.argv)
if __name__ == '__main__':
# Reading the qml file
engine = QQmlApplicationEngine()
context = engine.rootContext()
bridge = Bridge()
context.setContextProperty("con", bridge)
qmlFile = join(dirname(__file__), 'qml_test_1.qml')
engine.load(abspath(qmlFile))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
How can I know if the radio button is checked or not? I tried using objectName. But that does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用对象名称。这是不好的做法。使用信号,插槽或型号。
示例:
file main.py
file myradiobutton.qml
file main.qml
注意:
我建议您使用pyside6.3,因为它是官方QT绑定,它可以更好地支持 qml 。例如,在这里,您可能只是在Python中声明了一个属性,并将其绑定到“无线电”按钮文本。
Don't use object names. It is bad practice. Use signals and slots or models.
Example:
File main.py
File MyRadioButton.qml
File main.qml
Note:
I recommend you to use PySide6.3 since it is the official Qt binding and it has better support for QML. For instance, here you could have just declared a property in Python and bind it to the radio button text.