0

I am trying to create a table in PySide6 to show data from a database. One of the variables in my database is a boolean variable, and I would like that to show in the table as an editable checkbox, which I can simply click on to switch between True and False values.

This, I would have thought, should be a really easy thing to do, but as far as I can tell, it isn't.

Using QTableView, it appears to be phenomenally complicated. The question of how to do it has been asked many times before on this site and others (eg here), and seems to involve things like defining your own class to replace QTableView or adding a Delegate item for the relevant column, and quite possibly both. It always seems to involve many dozens of lines of code.

It does seem to be a lot simpler using QTableWidget instead of QTableView, and is simply a matter of doing something like this:

tbl = QTableWidget()
tbl.setCellWidget(row, col, QCheckBox())

(looks like you have to iterate through all the rows if you want it to apply to a whole column, but that's not too bad).

However, as I understand it, QTableWidget can't connect to a data model in the same way that QTableView can, so I'd have to write my own code to connect the QTableWidget to the data, and I'm back in "dozens of lines of code" territory.

Is it really that complicated, or am I missing something? It seems like it ought to be a very simple thing to do, but if it is, I haven't yet found the simple way of doing it.

BTW, I'm not yet totally wedded to PySide6. It seems like a nice GUI tool in many ways, but if someone is going to tell me that this is way simpler in Tkinter or wxPython or something like that, then I might be willing to consider switching to a different GUI framework.

Thanks

Adam Jacobs
  • 403
  • 4
  • 15
  • Subclassing is a fundamental part of Python and most importantly for Qt, so there's absolutely nothing wrong woth that practice. That said, doing that is actually quite easy, assuming the data model *does* allow it. Can you provide a [mre] with a basic sample of the database and its model? – musicamante Feb 10 '23 at 15:27
  • Sorry, no, I don't have an example. I haven't actually programmed this yet as it all seemed very complicated and I was hoping to find a simpler way. I have no problem with subclassing per se if it's just a matter of some small tweaks to 1 or 2 methods, but the examples I've seen (see for example the one I linked to in my OP) have quite substantial modifications to many methods, not just 1 or 2. – Adam Jacobs Feb 13 '23 at 10:18
  • Qt item models and views already support checkable items using the `Qt.CheckStateRole`. I don't know what model you are using, if you're using QStandardItemModel, just use `item.setCheckable(True)`, otherwise, for a QAbstractItemModel subclass, override `flags()` and also return `Qt.ItemIsUserCheckable` other than the default flags, and provide the implementations for the `Qt.CheckStateRole` both in `data()` and `setData()`. – musicamante Feb 13 '23 at 21:31

0 Answers0