-1

I'm trying to create an object (a QGroupBox with 2 QCheckBoxes inside) and replicate it in a 6x4 matrix. I need to access every QGroupBox label and every QCheckBox (and also other widgets i'm willing to put in here later) but i'm stuck in this place:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout, QGroupBox,
        QMenu, QPushButton, QRadioButton, QVBoxLayout, QWidget, QButtonGroup)

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

        self.channelsList = [] 
        
        grid = QGridLayout()
        for column in range(4):
            for item in range(6):
                #print(type(grid.addWidget(self.createGroup('CH ' + str(item + (column*6))), item, column)))
                print('CH ' + str(item + (column*6)) + ' Signal')
                relayChannel=('CH ' + str(item + (column*6)) + ' Signal')
                self.channelsList.insert(item,RelayChannel(relayChannel))               
                grid.addWidget(self.channelsList[item], item, column)              
                  
        self.setLayout(grid)

        self.setWindowTitle("PyQt5 Group Box")
        self.resize(400, 300)
    
    



class RelayChannel(QWidget):

    def __init__(self,relayChannel, parent=None):
        super(RelayChannel, self).__init__(parent)
        
    
    def __new__(self,relayChannel):
        groupBox = QGroupBox(relayChannel)

        self.cb_openLoad = QCheckBox("Open Load")
        self.cb_shortCircuit = QCheckBox("Short Circuit")
                
        self.cb_openLoad.clicked.connect(self.openLoadClicked)

        vbox = QVBoxLayout()
        vbox.addWidget(self.cb_openLoad)
        vbox.addWidget(self.cb_shortCircuit)
        
        vbox.addStretch(1)
        groupBox.setLayout(vbox) #prev vbox
 
        return groupBox

    def openLoadClicked(self):
        print(self)
        '''if self.cb_openLoad == 2: #openload is checked
            print('cb_OpenLoad checked')
            self.cb_shortCircuit.setChecked(False)'''

if __name__ == '__main__':
    app = QApplication(sys.argv)
    clock = RelayCommand()
    clock.show()
    
    print(clock.channelsList[0])
    
    sys.exit(app.exec_())

I'm taken it from an example i found on internet, but i can't connect properly the QCheckBoxes signals, since in def openLoadClicked(self): i see as self the value of the check box, but inside the function i can't access the other properties like self.cb_shortCircuit My suspect is that i have to declare all the things i putted in def __new__ in my init, but in this way i can't return the groupBox element and when i'm creating the matrix nothing happens

Help Please! Thx

cexco
  • 1
  • 1
    Yes, your usage of `__new__` is ***completely wrong***, and I really don't understand why you're doing it. Just change the inheritance to QGroupBox instead of QWidget, change the first line of the `__init__` to `super().__init__(relayChannel)`, remove **any** reference to `groupbox`, use `self.setLayout(vbox)` (`vbox = QVBoxLayout(self)`) and do **not** return anything from `__init__`. Then do serious research about the following topics: classes, instances, inheritance, attributes, differences between functions and methods, the meaning of `self` in Python and *proper* usage of dunder methods. – musicamante Aug 23 '23 at 08:50
  • The first argument(which you named as 'self' incorrectly) of the ```__new__()``` is a class(the ```RelayChannel```). So you connected the ```RelayChannel.openLoadClicked``` to the signal of the checkbox. – relent95 Aug 24 '23 at 01:42

0 Answers0