如何使用 pyqt5 從 .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_())
我的問題是如何知道單選按鈕是否被選中。我嘗試使用 objectName。但這不起作用。
uj5u.com熱心網友回復:
不要使用物件名稱,這是不好的做法。使用信號和插槽或模型。
例子:
main.py
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 App(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.current_rb_text = ""
@pyqtSlot(str)
def set_current_rb_text(self, text: str):
self.current_rb_text = text
@pyqtSlot()
def on_execute(self):
print(self.current_rb_text)
app = QGuiApplication(sys.argv)
if __name__ == '__main__':
# Reading the qml file
engine = QQmlApplicationEngine()
context = engine.rootContext()
bridge = App()
context.setContextProperty("App", bridge)
qmlFile = join(dirname(__file__), 'main.qml')
engine.load(abspath(qmlFile))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
MyRadioButton.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
RadioButton {id:btn
onClicked:App.set_current_rb_text(btn.text)
}
main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.14
ApplicationWindow {
visible: true
width: 600
height: 500
title: "HelloApp"
ColumnLayout{
MyRadioButton {
id: button1
text: "1"
objectName: "radio_button"
}
MyRadioButton {
id: button2
text: "2"
objectName: "radio_button"
}
MyRadioButton {
id: button3
text: "3"
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: {
App.on_execute()
}
}
}
注意:您使用的是非常舊的 pyqt5,我建議您使用 PySide6.3,因為它是官方的 Qt 系結并且它對 qml 有更好的支持,例如在這里您可以在 python 中宣告一個屬性并將其系結到單選按鈕文本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/496973.html
上一篇:從子小部件繼承信號