Squish/Enumerating all Objects Matching a Pattern: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
==Enumerating all objects matching a pattern==
[[Category:Tools::Squish]]


When using Squish’s pattern lookup functionality, it is often useful to perform an operation on all occurrences of an object matching a Squish pattern. The following function will let you enumerate all occurrences matching a pattern like you would any Python collection:
== Enumerating all objects matching a pattern ==


This function will yield all of the objects matching the specified pattern, by occurrence unless an alternative field to enumerate is provided with ‘%i’ as its value.<br />
When using Squish's pattern lookup functionality, it is often useful to perform an operation on all occurrences of an object matching a Squish pattern. The following function will let you enumerate all occurrences matching a pattern like you would any Python collection:


As a usage example, to log the contents of all '''QLabel''' widgets with parent :foo, you would do:<br />
This function will yield all of the objects matching the specified pattern, by occurrence unless an alternative field to enumerate is provided with '%i' as its value.<br /><code>def enumerateObjects(pattern):<br /> #allow %i to be specified manually, e.g. to check rows in a model one could do<br /> #enumerateObjects(&quot;{column='0' container=':containerObject' row='%i' type='QModelIndex'}&quot;)<br /> if '%i' not in pattern:<br /> if not pattern.endswith('}'): #invalid pattern<br /> raise RuntimeError(&quot;Pattern 's' is invalid!&quot; pattern)<br /> pattern = pattern[:–1] + &quot; occurrence='i'}&quot;<br /> i = 1<br /> while True:<br /> try:<br /> #return objects until a LookupError occurs<br /> yield findObject(pattern i)<br /> except:<br /> return<br /> i += 1<br /></code>


===Categories:===
As a usage example, to log the contents of all '''QLabel''' widgets with parent :foo, you would do:<br /><code>for label in enumerateObjects(&quot;{type='QLabel' parent=':foo'}&quot;):<br /> test.log(str(label.text()))
 
* [[:Category:Tools|Tools]]
** [[:Category:Tools::Squish|Squish]]

Revision as of 11:34, 24 February 2015


Enumerating all objects matching a pattern

When using Squish's pattern lookup functionality, it is often useful to perform an operation on all occurrences of an object matching a Squish pattern. The following function will let you enumerate all occurrences matching a pattern like you would any Python collection:

This function will yield all of the objects matching the specified pattern, by occurrence unless an alternative field to enumerate is provided with '%i' as its value.

def enumerateObjects(pattern):<br /> #allow %i to be specified manually, e.g. to check rows in a model one could do<br /> #enumerateObjects(&quot;{column='0' container=':containerObject' row='%i' type='QModelIndex'}&quot;)<br /> if '%i' not in pattern:<br /> if not pattern.endswith('}'): #invalid pattern<br /> raise RuntimeError(&quot;Pattern 's' is invalid!&quot; pattern)<br /> pattern = pattern[:1] + &quot; occurrence='i'}&quot;<br /> i = 1<br /> while True:<br /> try:<br /> #return objects until a LookupError occurs<br /> yield findObject(pattern i)<br /> except:<br /> return<br /> i += 1<br />

As a usage example, to log the contents of all QLabel widgets with parent :foo, you would do:
for label in enumerateObjects("{type='QLabel' parent=':foo'}"):
test.log(str(label.text()))