Qt 设计器 UI (python) 到 JSON [英] Qt Designer UI (python) to JSON

查看:55
本文介绍了Qt 设计器 UI (python) 到 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 qt 来构建 Python GUI.我有两个问题,我无法完全找到解决方案.下面的代码是我需要构建的示例.

I recently started using qt to build a python GUI. I have two problems I can't quite find the solutions to. the code below is a sample of what I need to build.

1:检查垂直布局中的单选按钮列表中的哪个单选按钮已被单击.在 GUI 中,它仅从布局中的所有其他可用单选按钮中选择一个.我怎么知道哪个被点击了?

1: Check which radio button from a list of radio buttons in a vertical layout has been clicked. In the GUI it only selects one radio button out of all others available in the layout. How do I perceive which has been clicked?

2:我想将单击的值添加到 JSON 对象中,但我相信这是一个简单的 if 语句,即 if this then that.除非它更复杂,在这种情况下,请把我推向正确的方向.

2:I would like to add the clicked value to a JSON object but I believe that is a simple if statement of if this then that. Unless it's more complicated in which case please push me in the right direction.

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(596, 466)
        self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(180, 70, 61, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.that = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.that.setObjectName("that")
        self.verticalLayout.addWidget(self.that)
        self.thi = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.thi.setObjectName("thi")
        self.verticalLayout.addWidget(self.thi)
        self.sure = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.sure.setObjectName("sure")
        self.verticalLayout.addWidget(self.sure)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.that.setText(_translate("Dialog", "that"))
        self.thi.setText(_translate("Dialog", "this"))
        self.sure.setText(_translate("Dialog", "sure"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

推荐答案

使用 Qt Designer 有一个很好的方法可以解决这个问题,它允许您将按钮分组到QButtonGroup,然后连接到它的buttonClicked 信号 获取被点击的按钮.

There is a nice way to solve this using Qt Designer, which allows you to group your buttons into a QButtonGroup, and then connect to its buttonClicked signal to get the button that was clicked.

您需要做的就是,在 Qt Designer 中,选择所有按钮(使用 Ctrl+单击),然后右键单击其中一个按钮并选择分配给按钮组 -> 新建按钮组.这将创建一个新的按钮组对象并自动将所有按钮添加到其中.

All you need to do is, in Qt Designer, select all the buttons (using Ctrl+click), then right-click one of the buttons and select Assign to button group -> New button group. This will create a new button-group object and automatically add all the buttons to it.

重新生成您的 gui 模块后,您可以执行以下操作:

After re-generating your gui module, you can then do somehting like this:

ui.radioButtonGroup.buttonClicked.connect(radioButtonClicked)

def radioButtonClicked(button):
    print(button.text())

这篇关于Qt 设计器 UI (python) 到 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆