how to query/test the state of a qt widget?

David Boddie david at boddie.org.uk
Sun Jun 24 09:52:46 EDT 2007


On Sun Jun 24 02:20:55 CEST 2007, raacampbell wrote:

> I'm writing a simple Python/Qt3 application and I am trying to write
> some code in which the user presses a button and the program performs
> action A or B depending upon the state of a pair of radio buttons.

You would typically connect the button's clicked() signal to a function
or method and perform the actions there. Since I don't know exactly what
you are doing, I can't give you an exact answer, but you probably want
to write something like this:

  self.connect(self.pushButton, SIGNAL("clicked()"), self.perform_action)

This assumes that you're calling connect() from a QObject subclass. If not,
you could call connect() on the button itself.

> I would therefore like Python to read the state of the buttons. I was
> expecting this to be straightforward but I've not been able to work
> out how to do it and searching on Google hasn't helped. Surely there's
> a one-liner that will do what I want? It seems like an every-day sort
> of problem. I'm after something like:
>
> if self.polPlotRadioButton.enabled==1: print "BLAH"

This is more or less what you would write in the method that gets invoked
when the clicked() signal is emitted, though you would need to call
enabled() since it's a method and not an attribute.

> I've found squish from www.froglogic.com but that seems over the top.
> Possibly pythonqt.sourceforge.net has something that will solve my
> problem but that wants Qt4 and at the moment I'm making heavy use of
> matplotlib widgets and I've not worked out how to get them to
> incorporate into a Qt4 app so I'm stuck with Qt3.

It's been a while since I've looked at using matplotlib with PyQt. Looking
at the 0.90.1 release, it seems that there is support for PyQt4, so maybe
you'll get what you need if you upgrade to that.

I don't think you need Squish for this - it's a tool for automated user
interface testing, and you're not doing that as far as I can tell.

David



More information about the Python-list mailing list