A common problem when developing test scripts for Qt application is that some table should be checked for whether it contains the correct data. In particular, the expected data is stored in an external file and the test script should load the file and then compare its contents against the table cells. Here’s one way to do it, in Python:
def checkTableAgainstTestdata( tableName, testDataFileName ): table = findObject( tableName ) dataset = testData.dataset( testDataFileName ) columns = table.columnCount rows = table.rowCount testDataColumns = len( testData.fieldNames( dataset[ 0 ] ) ) if columns != testDataColumns: test.fail( "Tables does not match test data", "Table " + tableName + " has different number" " of columns than test data in " + testDataFileName ) row = 0 for idx in dataset: for col in range( 0, columns ): if col >= testDataColumns: break expectedText = testData.field( idx, col ) tableText = table.item( row, col ).text() test.compare( expectedText, tableText ) row += 1 if row >= rows: test.fail( "Tables does not match test data", "Table " + tableName + " has different number" " of rows than test data in " + testDataFileName ) return if row >= rows: test.fail( "Tables does not match test data", "Table " + tableName + " has different number of" " rows than test data in " + testDataFileName ) return
The function simple takes the name of the table object to check as well as the name of the test data file. The test data file is expected to live in either the test cases “testdata” directory or in the shared testdata directory. A sample invocation might look like:
def main(): tableName = "{type='QTableWidget' visible='1'}" testDataFileName = "expectedvalues.tsv" checkTableAgainstTestdata( tableName, testDataFileName )
Note that this doesn’t work for Qt3 QTable objects or with plain QTableView objects. For those, the API for aquiring the contents of a specific table cell is different. However, the algorithm of the function is the same.
The above script looks good to check a dataset with the Qtable. I am working on Qt 4 Qtable objects and need to first save the Qtable in a file and then compare both the Qtables.
Can somebody tell me a python script/method to save a Qtable in a file.