Search code examples
pyqt4qradiobutton

How to shift position of radio button and reverse radio button function when unchecking it?


I would like to ask 2 questions about my code. First, how do I shift the position of my radio button into the position that I want (I wrote it in the code)? And how do I reverse my LCD screen after unchecking it? Right now, it shows '02' when checked but I want to reverse the process when I uncheck it. Any solution?

class MyRadioButton(QtGui.QRadioButton):
    def __init__(self):
        super(MyRadioButton, self).__init__()
        self.value = None

    def SetValue(self, val):
        self.value = val

def GetValue(self):
    return self.value

class UserTool(QtGui.QDialog):
    def __init__(self, parent = None):
        super(UserTool, self).__init__()
        self.layout = QtGui.QVBoxLayout(self)
        self.setup(self)        

    def setup(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        self.resize(688, 677)
        self.lcdNumber = QtGui.QLCDNumber(Dialog)
        self.lcdNumber.setGeometry(QtCore.QRect(590, 10, 71, 23))
        self.lcdNumber.setFrameShadow(QtGui.QFrame.Raised)
        self.lcdNumber.setObjectName(_fromUtf8("lcdNumber"))
        self.lcdNumber.setStyleSheet("* {background-color: black; color: white;}")
        self.lcdNumber.display("00")
        self.radioButton_8 = MyRadioButton()
        self.radioButton_8.setText("A1")
        self.radioButton_8.SetValue("02")
        self.radioButton_8.toggled.connect(self.showValueFromRadioButtonToLCDNumber)
        self.layout.addWidget(self.radioButton_8)
       #self.radioButton_8 = QtGui.QRadioButton(Dialog)
        self.radioButton_8.setGeometry(QtCore.QRect(460, 10, 82, 17))
        self.radioButton_8.setChecked(False)
        self.radioButton_8.setAutoExclusive(False)
        self.radioButton_8.setObjectName(_fromUtf8("radioButton_8")) 

    def retranslateUi(self, Dialog):
        self.radioButton_8.setText(_translate("Dialog", "A1", None))


def showValueFromRadioButtonToLCDNumber(self):
    value = self.radioButton_8.GetValue()
    if self.radioButton_8.isChecked():
        self.lcdNumber.display(value)

Solution

  • Here is working example. In order to show previous value you need mechanism to cash that value before it is changed. To shift radioButton I use addSpacing()

    # -*- coding: utf-8 -*-
    import sys
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtCore import Qt
    
    class MyRadioButton(QtGui.QRadioButton):
        def __init__(self):
            super(MyRadioButton, self).__init__()
            self.value = None
    
        def SetValue(self, val):
            self.value = val
    
        def GetValue(self):
            return self.value
    
    class Widget(QtGui.QWidget):
        def __init__(self):
            super(Widget, self).__init__()
            self.layout = QtGui.QVBoxLayout(self)
    
            self.radioButton = MyRadioButton()
            self.radioButton.setText("some text")
            self.radioButton.SetValue("02")
            self.radioButton.toggled.connect(self.showValueFromRadioButtonToLCDNumber)
    
            self.lcdNumber = QtGui.QLCDNumber()
            self.lcdNumber.display("-2")# previous value
    
            self.layoutHorizontal = QtGui.QHBoxLayout(self)
            self.layoutHorizontal.addSpacing(20)# add space before radioButton
            self.layoutHorizontal.addWidget(self.radioButton)
    
            self.layout.addLayout(self.layoutHorizontal)
            self.layout.addWidget(self.lcdNumber)
    
            self.previousValue = ""
    
        def showValueFromRadioButtonToLCDNumber(self):
            value = self.radioButton.GetValue()
            if self.radioButton.isChecked():
                self.previousValue = self.lcdNumber.value()# save previous value before it is changed
                self.lcdNumber.display(value)
            else:
                self.lcdNumber.display(self.previousValue)