1Z10
1Z10

Reputation: 3341

PyQt5 QTableView: how to disable user interaction/selection while keeping the default style/colors?

I need to be able to programmatically select some rows of a TableView, hence showing the selected rows to the user. Of course the user should not be able to change the selected rows by clicking here and there.

Currently, the only way I found to disable the user's interaction is with:

self.table_view.setEnabled(False)

Altough this resolves the user's selection problem, it also changes the aspect of the table, making it all greyed out:

enter image description here

How can I restore the original style, with the selected rows displaying as blue? enter image description here

Upvotes: 1

Views: 2703

Answers (2)

akushyn
akushyn

Reputation: 103

If I correctly understand your question, you have to play with properties :

  • setSelectionMode()
  • setSelectionBehavior()

    table_view.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
    table_view.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
    

Upvotes: 1

aseylys
aseylys

Reputation: 344

Open up QDesigner and load your .ui file. Click on your QTableView and in the Property Editor (right side panel) scroll down to the purple area. There you will see editTriggers, expand that. Check the first one that says NoEditTriggers.

This will disable the user from editing cells in the table. Let me know if my instructions weren't clear enough and I can provide pics.

Upvotes: 2

Related Questions