Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Move [[Category::Tools::Squish]] -> [[Category::Squish]])
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Tools::Squish]]
[[Category:Squish]]


== Enumerating all objects matching a pattern ==
== Enumerating all objects matching a pattern ==
Line 5: Line 5:
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:
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.<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("{column='0' container=':containerObject' row='%i' type='QModelIndex'}")<br /> if '%i' not in pattern:<br /> if not pattern.endswith('}'): #invalid pattern<br /> raise RuntimeError("Pattern 's' is invalid!" pattern)<br /> pattern = pattern[:–1] + " occurrence='i'}"<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>
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.
<code>def enumerateObjects(pattern):
#allow %i to be specified manually, e.g. to check rows in a model one could do
#enumerateObjects("{column='0' container=':containerObject' row='%i' type='QModelIndex'}")
if '%i' not in pattern:
if not pattern.endswith('}'): #invalid pattern
raise RuntimeError("Pattern 's' is invalid!" pattern)
pattern = pattern[:–1] + " occurrence='i'}"
i = 1
while True:
try:
#return objects until a LookupError occurs
yield findObject(pattern i)
except:
return
i += 1
</code>


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

Latest revision as of 08:37, 25 November 2017


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

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()))