Squish/Robust Selection of View Items with Variable Text

From Qt Wiki
< Squish
Revision as of 17:39, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Robust selection of items in a tree or list view with variable text

This article is similiar to to Finding list items that ends in a given string however goes into a bit more detail and is also functional for treeviews as well as listviews. Dealing with a treeview requires careful handling of “.”-separated paths.

Imagine you need to select some item in a treeview, but you don’t know the exact string of the item as it may have changed. Alternatively maybe the parent item of the item you wish to select has changed, but the item is still the same. You need to find the item you want in the tree, and extract the correct Squish item identifier to interact with it later on.

For example, assume you need to find a certain file in treeview, but the treeview has items with absolute paths that might be different on various machines. All you know is that the filename you are looking for will be the same, if it exists. You wish to iterate through the treeview, checking each level against some known valid path that you have. For this example, we will assume you have paths such as

and you can’t be sure which path is in the treeview (potentially as a parent or child item). This means you might need to convert

to

(Note that these paths are escaped so that Squish’s itemview functions properly find the paths). As you can see, all three of the paths are the same from the “test_data” folder onwards, so we will use that to split between the “known good” parts of the path we wish to match, and the variable part we wish to replace.

As is visible in the above code sample, this code only searches three-levels-deep for a match. If your tree is deeper than 3 levels, you’ll need to either add another level of drilling down, or convert it into recursive function that works on any arbitrary level. The above code can be used like so:

Categories: