![]() |
PyQt Changing multiple Objects attributes at once
Hi Guys
I have over 100 QpushButtons in a gridlayout, and I want to set the toggle state of all of them to the same state. Any clues how I can do this short of writing this statement 100 times self.pushButton1.setChecked(0) . . . self.pushButton100.setChecked(0) Cheers Mike C |
Re: PyQt Changing multiple Objects attributes at once
Maybe (most likely?) I am missing something obvious, but why don't you use array of push buttons and iterate through them in a loop?
Hartti |
Re: PyQt Changing multiple Objects attributes at once
Quote:
also i thought there might be a more clever way, ie changing the property of all instances of QpushButton. thanks in advance |
Re: PyQt Changing multiple Objects attributes at once
Quote:
|
Re: PyQt Chaning multiple Objects attributes at once
Pelago is right. However, since you already did it the other way, you're probably looking for the eval function, something like
eval("pushButton%s.setChecked(0)" % i) |
Re: PyQt Chaning multiple Objects attributes at once
Thanks Atilla and Pelago
I will give the eval function a try, sounds like exec() might also be useful. Is there no way to globally set QpushButton attributes? for instance I can change the style of QpushButtons globally with a single method |
Re: PyQt Chaning multiple Objects attributes at once
Quote:
|
Re: PyQt Chaning multiple Objects attributes at once
ok just to close close this thread off.
Could not get Eval() to work but Exec() worked well , but I decided to rewrite the app using an array of buttons populated from within the application. This was non trivial cause of python arrays peculiarities but here is the code snippet for future ref. This creates an 8x16 array of buttons in an array declared inside the Qt MainWindow Class (this avoids the use of globals, don't even go there :D) Code:
#Here is the MainWindow Class with an array called ledArray declared.Code:
def initled(): Code:
for r in range(8): http://farm3.static.flickr.com/2799/...2bf5432a_o.png Mike C |
Re: PyQt Chaning multiple Objects attributes at once
Quote:
|
Re: PyQt Chaning multiple Objects attributes at once
Quote:
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 |
Re: PyQt Chaning multiple Objects attributes at once
Quote:
Code:
tempPushButton = QtGui.QPushButton() |
Re: PyQt Chaning multiple Objects attributes at once
Quote:
|
Re: PyQt Chaning multiple Objects attributes at once
Quote:
The Pythonic way rocks, so much easier when you know how :D |
Re: PyQt Chaning multiple Objects attributes at once
I assume list comprehensions aren't used to keep things simple/terse?
|
Re: PyQt Chaning multiple Objects attributes at once
Quote:
|
Re: PyQt Chaning multiple Objects attributes at once
Quote:
I can already hear the question - but then I need to create an array and append all of them... yuck, that's no better than the original ! But before I address that valid concern, let's let's dig a little deeper into the topic of Python-style garbage collection. Take a look at this: Code:
// C++This also explains why you don't get a segfault in your original example - by calling the layout's addWidget before the QPushButton you created a reference that prevents garbage collection. Without this, if you came back to it after the original QPushButton pointer went out of scope (or the reference disappeared for whatever ownership reason), you would get a big fat segfault because (unlike in C++) the underlying object is no longer there. So the alternative solution to local variables (which are ugly if you have many objects) is to specify parents in the constructor, creating the necessary reference to avoid garbage collection (i.e. mylist.append(QPushButton(parent))). Usually you create enough references inadvertently (like here with addWidget, or QObject voodoo that happens behind the scenes) for garbage collection/ownership not to mess you up, but sooner or later, especially in applications with multiple intertwined classes, you *will* run into segfaults or weirdness because of this. Seems we got into deep water here, but the idea is not to get surprised when you meet your first segfaults and know where to look for trouble spots :) |
Re: PyQt Chaning multiple Objects attributes at once
Quote:
EDIT: just for the sake of completeness: Code:
[[led.setChecked(self.ledState) for led in row] for row in self.ledArray] |
| All times are GMT. The time now is 22:24. |
vBulletin® Version 3.8.8