Squish/Iterating Through a Table: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
this page in: [[:Category:Tools::Squish::IteratingThroughATableSpanish|Español]]
this page in: [[Category:Tools::Squish::IteratingThroughATableSpanish|Español]]


=Iterating through a table=
[[Category:Tools::Squish]]
 
= Iterating through a table =


There are several ways to iterate through a table in your test script to be used when you need to verify or take action on a given set of rows or columns. In the examples below we will use all three approaches to verify that there are no empty cells in a table.
There are several ways to iterate through a table in your test script to be used when you need to verify or take action on a given set of rows or columns. In the examples below we will use all three approaches to verify that there are no empty cells in a table.


In the end, the usage of any of these three approaches would simply be:<br />
In the end, the usage of any of these three approaches would simply be:<br /><code>verifyNoEmptyCells(&quot;:MyQTableWidget&amp;quot;)<code>


==Qt <span class="caps">API</span> approach==
== Qt API approach ==


This approach uses the Qt <span class="caps">API</span> to access the model directly and iterate through the rows and columns of the table.
This approach uses the Qt API to access the model directly and iterate through the rows and columns of the table.


==waitForObjectItem()==
</code>def verifyNoEmptyCells(tableName):<br /> table = waitForObject(tableName)<br /> model = table.model()<br /> columnCount = model.columnCount()<br /> rowCount = model.rowCount()


Using waitForObjectItem() you can access items of a specific widget, such as actions in a menu or, in this case, cells in a table. It takes two parameters, the first being the name of the parent object and the second being the identification for the item. For cells in a table, that is two integers in the form of row/col.
for col in range(columnCount):<br /> for row in range(rowCount):<br /> itemText = str(model.data(model.index(row, col), Qt.DisplayRole).toString())<br /> test.verify(itemText != &quot;&quot;)<code>


==Real name approach==
== waitForObjectItem() ==


Squish offers two ways of identifying your objects in a test script, either using the symbolic name which maps to an entry in the object map, or using the real name which lists all properties and their expected value in an identification string. Behind the scenes, the symbolic name of course maps to this real name, using the symbolic name generally makes the scripts more readable though.
Using waitForObjectItem() you can access items of a specific widget, such as actions in a menu or, in this case, cells in a table. It takes two parameters, the first being the name of the parent object and the second being the identification for the item. For cells in a table, that is two integers in the form of row/col.
 
By using the real name to identify cells, we can change the expected values of the properties and hence access different table cells as seen below.
 
===There is only one article in "Tools -&gt; Squish -&gt; Iterating_through_a_table":===
 
===T===
* [[:Category:Tools::Squish::IteratingThroughATableSpanish|Category:Tools -&gt; Squish -&gt; IteratingThroughATableSpanish]]


===Categories:===
</code>def verifyNoEmptyCells(tableName):<br /> table = waitForObject(tableName)<br /> columnCount = table.columnCount<br /> rowCount = table.rowCount


* [[:Category:Tools|Tools]]
for col in range(columnCount):<br /> for row in range(rowCount):<br /> item = waitForObjectItem(tableName, str(row)+&quot;/&amp;quot;''str(col))<br /> test.verify(item.text != &quot;&quot;)<code>
** [[:Category:Tools::Squish|Squish]]
<br />h2. Real name approach
* [[:Category:Tools::Squish::IteratingThroughATableSpanish|IteratingThroughATableSpanish]]
<br />Squish offers two ways of identifying your objects in a test script, either using the symbolic name which maps to an entry in the object map, or using the real name which lists all properties and their expected value in an identification string. Behind the scenes, the symbolic name of course maps to this real name, using the symbolic name generally makes the scripts more readable though.
<br />By using the real name to identify cells, we can change the expected values of the properties and hence access different table cells as seen below.
<br /></code>def verifyNoEmptyCells(tableName):<br /> waitFor(&quot;object.exists(tableName)&quot;)<br /> columnCount = findObject(tableName).columnCount<br /> rowCount = findObject(tableName).rowCount
<br /> for col in range(columnCount):<br /> for row in range(rowCount):<br /> item = waitForObject(&quot;{column='&quot;'' str(col) + &quot;' container='&quot; + tableName + &quot;' row='&quot; + str(row) + &quot;' type='QModelIndex'}&quot;)<br /> test.verify(item.text != &quot;&quot;)<code>

Revision as of 10:08, 24 February 2015

this page in:

Iterating through a table

There are several ways to iterate through a table in your test script to be used when you need to verify or take action on a given set of rows or columns. In the examples below we will use all three approaches to verify that there are no empty cells in a table.

In the end, the usage of any of these three approaches would simply be:

verifyNoEmptyCells(&quot;:MyQTableWidget&amp;quot;)<code>

== Qt API approach ==

This approach uses the Qt API to access the model directly and iterate through the rows and columns of the table.

def verifyNoEmptyCells(tableName):
table = waitForObject(tableName)
model = table.model()
columnCount = model.columnCount()
rowCount = model.rowCount() for col in range(columnCount):
for row in range(rowCount):
itemText = str(model.data(model.index(row, col), Qt.DisplayRole).toString())
test.verify(itemText != "")

== waitForObjectItem() ==

Using waitForObjectItem() you can access items of a specific widget, such as actions in a menu or, in this case, cells in a table. It takes two parameters, the first being the name of the parent object and the second being the identification for the item. For cells in a table, that is two integers in the form of row/col.

def verifyNoEmptyCells(tableName):
table = waitForObject(tableName)
columnCount = table.columnCount
rowCount = table.rowCount for col in range(columnCount):
for row in range(rowCount):
item = waitForObjectItem(tableName, str(row)+"/&quot;str(col))
test.verify(item.text != "")

<br />h2. Real name approach
<br />Squish offers two ways of identifying your objects in a test script, either using the symbolic name which maps to an entry in the object map, or using the real name which lists all properties and their expected value in an identification string. Behind the scenes, the symbolic name of course maps to this real name, using the symbolic name generally makes the scripts more readable though.
<br />By using the real name to identify cells, we can change the expected values of the properties and hence access different table cells as seen below.
<br />

def verifyNoEmptyCells(tableName):
waitFor("object.exists(tableName)")
columnCount = findObject(tableName).columnCount
rowCount = findObject(tableName).rowCount


for col in range(columnCount):
for row in range(rowCount):
item = waitForObject("{column='" str(col) + "' container='" + tableName + "' row='" + str(row) + "' type='QModelIndex'}")
test.verify(item.text != "")