如何索引QGridLayout内的小部件

时间:2017-01-16 08:56:06

标签: python layout pyside qgridlayout

我试图用Python3在PySide中创建一个小部件矩阵。该创建已经开始,但我无法更新小部件。

我创建了一个QGridLayout,然后添加了带有初始文本的标签小部件。在此之后,我构建了一个包含最后一个布局的30个实例的列表。然后我只是在新的QGridLayout内添加每个布局,因此我创建了一个带有小部件的布局矩阵。

创建工作正常,但之后我无法再索引或指向布局的每个小部件。我怎么能这样做?

我的问题是当我尝试做的时候:

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

我明白了:

  

错误:userSpace没有userHeartRate属性

代码:

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

0 个答案:

没有答案