Search code examples
pythonlayoutpyqt4signals-slots

How to uncheck all radio buttons from another class


I want to uncheck some radio buttons in class A by pushing a button in class B.

My example codes are as below:

import sys, os

import PyQt4
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget1(QWidget):
    def __init__(self, bridge, parent=None):
        super().__init__()

        self.bridge = bridge

        self.grid = QGridLayout()

        self.radiobtn = QRadioButton()
        self.grid.addWidget(self.radiobtn, 0, 0)

class Widget2(QWidget):

    def __init__(self, parent = None):
        super().__init__()

        self.grid = QGridLayout()

        self.pushbtn = QPushButton()
        self.grid.addWidget(self.pushbtn,0, 0)

class Tab1Layout(QWidget):
    def __init__(self, parent = None):
        super().__init__()
        self.grid = QGridLayout()

        self.group2 = Widget2()

        self.group1 = Widget1(self.group2, self)

        self.grid.addWidget(self.group1, 0, 0)
        self.grid.addWidget(self.group2, 1, 0)


class Tabs(QTabWidget):
    def __init__(self, parent = None):
        super().__init__()
        self.tab1 = Tab1Layout()
        self.addTab(self.tab1, 'Tab1')

        self.show()

def main():
    app = QApplication(sys.argv)
    main = Tabs()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

As my code is now, it has two problems:

  1. The radio button and the push button don't show up

  2. Let's say the radio button and the push button finally show up: I want that when I click the push button in Widget2, the radio button in Widget1 should be unchecked if it is already checked.

Please note that I'm already linking Widget1 and Widget2 by putting a second argument bridge in Widget1. The reason I did this is because there's some other functions in my original project. So please don't alter this part if possible.


Solution

  • The buttons don't show up because you have not added the grid layouts to any of the widgets. So for all three grid layouts, do either this:

        self.grid = QGridLayout()
        self.setLayout(self.grid)
    

    or this:

        self.grid = QGridLayout(self)
    

    To check/uncheck the radio button using a button click, do this:

    class Tab1Layout(QWidget):
        def __init__(self, parent = None):
            ...
            self.group2.pushbtn.clicked.connect(self.group1.radiobtn.toggle)