0

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)
Viv91
  • 45
  • 2
  • 8
  • do you need LCDnumber to show previous value or simply to clear value? is that what you meant by "reverse the process"? – Aleksandar Jul 18 '14 at 10:25
  • Show previous value. For example, the radiobutton will display value of 02 when checked and then minus the value of 02 when unchecked. – Viv91 Jul 21 '14 at 01:39
  • is there a command to shift position of the radio button as well? I realise that `self.radioButton_8.setGeometry(QtCore.QRect(460, 10, 82, 17))` does not have any effect in QVBoxLayout. – Viv91 Jul 21 '14 at 01:44

1 Answers1

0

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)
Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • I used your code but it only works for one radio button. I want it to work for multiple radio buttons. Checked radio buttons will contribute their value to be added up and their final result displayed in the lcd. Unchecking some radio buttons will then subtract their respective values from the lcd. I am trying to figure it out now. – Viv91 Jul 21 '14 at 07:47
  • I have posted it as a new question to be clearer. The addSpacing() did not work much for me but instead I added all my radio buttons into QHBoxLayout and that almost work as what I wanted. – Viv91 Jul 21 '14 at 08:31
  • if you use addSpacing(20) on QHBoxLayout (as I did) it will add 20pix space on x. Be sure to add horizontal layout to your main layout: self.layout.addLayout(self.layoutHorizontal). Also order is important, first do self.layoutHorizontal.addSpacing(20), second do this self.layoutHorizontal.addWidget(self.radioButton) – Aleksandar Jul 21 '14 at 08:40
  • 1
    Yes, I did like what you did in the code but my radio buttons ended up overlapping other widgets. I think I have to adjust the spacing between them so I used `self.layout.setSpacing(10)` but nothing changed. So, in the end I added the whole row of widgets into the layout and shift them together as one. It looks okay now. – Viv91 Jul 21 '14 at 08:45