Qt Designer UI (python) to JSON
Asked Answered
R

2

0

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_())
Rudder answered 25/1, 2017 at 0:28 Comment(0)
M
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())
Martynne answered 25/1, 2017 at 21:22 Comment(4)
this seems like the best option and it's what i will probably use. Thanks!Rudder
okay so bigger problem, When I use this it crashes the entire GUI. I get a pop-up saying that python stopped working. Have you encountered this problem before? Working in python 3.5.2 and qt5Rudder
@Haq.H. Did you re-generate the gui module with pyuic? If not, the problem must be in your own code somewhere.Martynne
I did I re-generated the new .py file, there really isn't an error message so I don't know where to look I just made a post with more details if you would like to take a look at at it #41926122Rudder
C
0

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
Conscientious answered 25/1, 2017 at 1:7 Comment(1)
I'll take a look, I came to a similar conclusion however, without using QtCore.SIGNAL what is it used for?Rudder

© 2022 - 2024 — McMap. All rights reserved.