Search code examples
pythonpyqt4qtableview

PyQt4 - QTableView - How to loop over QTableView


I tried to find an example in python where I can loop over model elements of a QTableView and print the entire row. I have found how to loop over selected rows but nothing when rows are not selected.

Can someone help me? No need to tell me how to create the model and how to apply it to the QTableModel. I am only interested how to iterate over the rows.


Solution

  • I think you confused model and view in some places within your question...

    But, why not simply get the number of rows and columns and loop over all of them:

    for irow in xrange(model.rowCount()):
        row = []
        for icol in xrange(model.columnCount()):
            cell = model.data(model.createIndex(irow, icol))
            row.append(cell)
        # print all elems per row
        print ', '.join(str(c) for c in row))
    

    Things can probably be beautified and/or written in a more compact way, but that's basically all. Or am I missing something?