1

I'm trying to create a matrix of widgets in PySide with Python3. The creation is working already but I cannot update the widgets.

I create a QGridLayout, then I add label widgets with an initial text. After this I build a list of 30 instances of the last layout. Then I just add each layout inside of a new QGridLayout, and so I create a matrix of layouts with widgets.

The creation works ok, but after that I can no longer index or point to each widget of the layout. How could I do that?

My problem is when I try to do:

self.userSpace[i].userHeartRate.setText('lala')

I get:

Error: userSpace doesn't have userHeartRate attribute

Code:

class MainWindow(PySide.QtGui.QWidget):
    # Constructor function
    def __init__(self):
        super(MainWindow, self).__init__()

        self.socios=[]

        #Cuadritos
        self.userSpace = []        

        #Matriz
        self.gridLayout = PySide.QtGui.QGridLayout(self)

        self.initGUI()

    def initGUI(self):
        self.setGeometry(0, 0, 1920, 1080)
        self.setMinimumHeight(1080)
        self.setMinimumWidth(1920)
        self.setMaximumHeight(1080)
        self.setMaximumWidth(1920)

        #Layout creation
        self.setGroupLayout() 

        #Allways show at last 
        self.show()

    #general users layout
    def setGroupLayout(self):
        for i in range(1,31):
            self.socios.append(i)

        #Creates a list of gridlayouts
        for i, socio in enumerate(self.socios):        
            self.userSpace.append(self.setUserLayout(str(socio)))

            #This is the problem, I cannot index any widget in the layout
            self.userSpace[i].userHeartRate.setText('lala') #This is not working
            #userSpace doesn't have userHeartRate attribute

         #Later I'll add each gridlayout inside another gridlayout to create a matrix of the first layouts   

        for vertical in range (0,5):
            for horizontal in range(0,6):
                if usrNumMain == tempCounter:
                    break
                else:
                    self.gridLayout.addLayout(self.userSpace[tempCounter], vertical, horizontal, PySide.QtCore.Qt.AlignCenter)                    

                    tempCounter = tempCounter + 1                    

    def setUserLayout(self, name):

        userGridLayout = PySide.QtGui.QGridLayout()

        #User Name widget
        userName = PySide.QtGui.QLabel(name)
        userHeartRate = PySide.QtGui.QLabel("FC")
        userGridLayout.addWidget(userName, 0, 1, 1, 4)
        userGridLayout.addWidget(userHeartRate, 3, 1, 1, 2)

        return userGridLayout
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
EinSoldiatGott
  • 63
  • 1
  • 11
  • You could keep track of the widgets with some other data structure or you could look at the children of the nexted layouts to get the widgets of interest. – NoDataDumpNoContribution Jan 18 '17 at 12:03
  • Now what I'm doing is placing every widget in a list, it works but not sure if it's the best way for it, is it? – EinSoldiatGott Jan 19 '17 at 00:39
  • The best way depends on your specific problem, what you want to do with the widgets and how often you need to find them. Keeping them in an additional list is surely not the worst way of accessing them. – NoDataDumpNoContribution Jan 19 '17 at 07:38

0 Answers0