View Single Post
Posts: 3,319 | Thanked: 5,610 times | Joined on Aug 2008 @ Finland
#10
Originally Posted by mikec View Post
Code:
def initled():        
        for r in range(8): 
            MainWindow.ledArray.append([])
            for c in range(16):
                    MainWindow.ledArray[r].append(QtGui.QPushButton())
                    MainWindow.ledArray[r][c].setCheckable(True) 
                    MainWindow.ledArray[r][c].setMinimumSize(QtCore.QSize(45, 45)) 
                    ui.gridLayout.addWidget(MainWindow.ledArray[r][c], r, c)
Great stuff ! Two notes, though (not as discouragement, just as advices):

Avoid things like MainWindow.ledArray[r].append(QtGui.QPushButton())

The problem is that Python handles Qt object lifetimes/references differently than C++ and this (calling a constructor as a parameter for another function) can cause all sorts of nastiness if you're not careful. I know it's uglier to allocate a local variable for this, but you'll understand when you meet the first segfaults

The other note is python style - when dealing arrays it's recommended to use the 'in' operator. Faster, IMHO prettier and less error-prone than index based stuff. So, something like:

Code:
# c-ish
for r in range(8): 
            for c in range(16):
                self.ledArray[r][c].setChecked(self.ledState)

# pythonic
for row in self.ledArray: 
            for led in row:
                led.setChecked(self.ledState)
See ?
__________________
Blogging about mobile linux - The Penguin Moves!
Maintainer of PyQt (see introduction and docs), AppWatch, QuickBrownFox, etc
 

The Following 2 Users Say Thank You to attila77 For This Useful Post: