Qt Designer UI (python) to JSON

家住魔仙堡 提交于 2019-12-12 03:47:15

问题


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

回答1:


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.

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.

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

ui.radioButtonGroup.buttonClicked.connect(radioButtonClicked)

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



回答2:


I think you need something like this (not tested)

# Set Default
self.thi.setChecked(True)

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

then create a function

def self.radio_clicked(self):
    print 'ive been clicked' # work from here


来源:https://stackoverflow.com/questions/41841414/qt-designer-ui-python-to-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!