Qt Designer UI (python) 到 JSON

问题描述 投票:0回答:2

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

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

2:我想将单击的值添加到 JSON 对象,但我相信这是一个简单的 if 语句 if this then that。除非情况更复杂,否则请将我推向正确的方向。

# -*- 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_())
python pyqt qt-designer signals-slots qradiobutton
2个回答
0
投票

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

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

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

ui.radioButtonGroup.buttonClicked.connect(radioButtonClicked)

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

0
投票

我认为你需要这样的东西(未经测试)

# Set Default
self.thi.setChecked(True)

# create a signal
QtCore.QObject.connect(self.that,
                       QtCore.SIGNAL("toggled(bool)"),
                       self.radio_clicked)

然后创建一个函数

def self.radio_clicked(self):
    print 'ive been clicked' # work from here
© www.soinside.com 2019 - 2024. All rights reserved.