使用PYQT 5从.QML文件获取单选按钮值

发布于 2025-02-10 06:55:17 字数 1649 浏览 1 评论 0原文

如何使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2025-02-17 06:55:17

不要使用对象名称。这是不好的做法。使用信号,插槽或型号。


示例:

file 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_())

file myradiobutton.qml

import QtQuick 2.15
import QtQuick.Controls 2.15


RadioButton {id:btn
    onClicked:App.set_current_rb_text(btn.text)
}

file 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()
        }
    }

}

注意:

我建议您使用pyside6.3,因为它是官方QT绑定,它可以更好地支持 qml 。例如,在这里,您可能只是在Python中声明了一个属性,并将其绑定到“无线电”按钮文本。

Don't use object names. It is bad practice. Use signals and slots or models.


Example:

File 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_())

File MyRadioButton.qml

import QtQuick 2.15
import QtQuick.Controls 2.15


RadioButton {id:btn
    onClicked:App.set_current_rb_text(btn.text)
}

File 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()
        }
    }

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文