Iterate through the contents of rows selected from a QTableView QSqlQueryModel?

Heads up! You've already completed this tutorial.

luis | 2020-10-02 21:46:29 UTC | #1

I'm going through the "Querying SQL databases with Qt models" chapter of the PySide2 edition of the book. I need something like the "databases\" sample. And then a button, that should perform something based on the contents of the rows selected by the user from that tableview.

So, how can I get the rows that were selected by the user from that tableview and then iterate through the contents of those selected rows?

Thanks in advance!

luis | 2020-10-01 22:22:36 UTC | #2

I found the answer. On my self.table = QTableView() I need to do this the retrieve the rows selected by the user:

indexes = self.table.selectionModel().selectedRows()

and then for example to print the contents of the 2nd column of the selected rows:

for index in indexes:
            print(self.table.model().data(self.table.model().index(index.row(), 1)))

martin | 2020-10-03 12:03:28 UTC | #3

Hey @luis yep that will do it. I'd take a reference to the model object first just to simplify the code a bit

model = self.table.model()
for index in indexes:
            print(, 1)))

~~Note, that in your .data method the second param 1 is Qt.DecorationRole -- not sure if this is what you intended? If you want to get the content you would use Qt.DisplayRole (which == 0) (see roles enum in the documentation).~~ Ignore this my eyes were wonky with the brackets.

Also .... I was looking at the selectedRows method though and noticed that it actually accepts a parameter column which (slightly confusingly) is the value it uses for the column in the returned indexes.

Since you want the values in column 2, you can simplify a bit by passing that column number to selectedRows directly... it then returns the exact indexes you want.

indexes = self.table.selectionModel().selectedRows(column=2)
model = self.table.model()
role = Qt.DisplayRole # or Qt.DecorationRole
for index in indexes:
    print(, role))

luis | 2020-10-02 14:03:16 UTC | #4

Much cleaner! Thanks for the hints.

edit: and in my code, the 1 in index.row(), 1 is needed to access the data of the second column. 0 gives me the 1st column, 2 would be the 3rd and so on.

martin | 2020-10-02 21:45:49 UTC | #5

Great, glad it helped! I think this is a good candidate to add tutorial about, common to want to find what is selected in a table.

[quote="luis, post:4, topic:495"] edit: and in my code, the 1 in index.row(), 1 is needed to access the data of the second column. 0 gives me the 1st column, 2 would be the 3rd and so on. [/quote]

Ha, quite right -- got confused among all the brackets :D

Max_Fritzler | 2021-05-01 21:15:17 UTC | #7

I would really appreciate a tutorial on this.

Also, you can apparently do something like: "self.myQSQLTableModel.record(integer).value("myFieldName")

I'm trying to use this because it allows reference by name. I hate using positional reference to a sql record, because it breaks if I find myself having to add or delete fields. This apparently acts through the model, whereas your solution Martin acts through the TableView. Since I'm a newbie with QT, I have not yet figured out when I should be using the model, and when the view.


Max_Fritzler | 2021-05-02 10:45:15 UTC | #8

and of course, Martin, in your example I can use fieldIndex to get the value by name, like this: indexes = self.utterancesTable.selectionModel().selectedRows(column=self.model.fieldIndex("UID"))

Maxim_Doucet | 2021-07-16 12:59:35 UTC | #9

You can also get the same result with:

indexes = self.table.selectionModel().selectedRows()
for index in indexes:
    print(index.child(index.row(), 1).data())

(no need to retrieve the model, the index is enough)

Over 10,000 developers have bought Create GUI Applications with Python & Qt!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ ]]% discount with the code [[ couponCode ]] — Enjoy!

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak