![]() |
2010-03-12
, 12:23
|
|
Posts: 2,121 |
Thanked: 1,540 times |
Joined on Mar 2008
@ Oxford, UK
|
#11
|
![]() |
2010-03-12
, 12:59
|
Posts: 3,319 |
Thanked: 5,610 times |
Joined on Aug 2008
@ Finland
|
#12
|
Hi, I'm not the OP but I'm trying to learn PyQt. Do you mean that
is the thing to do? Is there a reference as to why the first form is incorrect?Code:tempPushButton = QtGui.QPushButton() MainWindow.ledArray[r].append(tempPushButton)
![]() |
2010-03-12
, 19:35
|
|
Posts: 1,366 |
Thanked: 1,185 times |
Joined on Jan 2006
|
#13
|
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:
See ?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)
![]() |
2010-03-13
, 09:20
|
Posts: 402 |
Thanked: 229 times |
Joined on Nov 2009
@ Missouri, USA
|
#14
|
![]() |
2010-03-13
, 09:34
|
|
Posts: 1,366 |
Thanked: 1,185 times |
Joined on Jan 2006
|
#15
|
Hi, I'm not the OP but I'm trying to learn PyQt. Do you mean that
is the thing to do? Is there a reference as to why the first form is incorrect?Code:tempPushButton = QtGui.QPushButton() MainWindow.ledArray[r].append(tempPushButton)
![]() |
2010-03-13
, 10:51
|
Posts: 3,319 |
Thanked: 5,610 times |
Joined on Aug 2008
@ Finland
|
#16
|
Just tried this, and it does not work. you end up appending the same pushbutton into the array, rather than an instance of the qpushbutton. This has got me curious
// C++ col = new QColor(); col = new QColor(); # Python col = QtGui.QColor() col = QtGui.QColor()
![]() |
2010-03-13
, 10:58
|
Posts: 3,319 |
Thanked: 5,610 times |
Joined on Aug 2008
@ Finland
|
#17
|
I assume list comprehensions aren't used to keep things simple/terse?
[[led.setChecked(self.ledState) for led in row] for row in self.ledArray]