|
|
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)
AFAIK the problem stems from two sources - from the way Python garbage collection works and from the problem that it does not necessarily know about object pointer ownership transfers than happen on Qt's level. Can't find anything descriptive on google right now, so you'll have to take my word for it
Or remember this and when your first segfault appears, try the suggested change above and then ponder why it fixes the problem
|
|
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()
The point is that in case of C++, the first created object is still there after the second line, even though it is no longer directly addressable. In Python, however, the events of the second line mean the object will be garbage collected as there are no references left to it ! Now, to curve back to the original problem, we can solve this by increasing the reference count by hierarchical means, by specifying an explicit parent object.
|
|
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]