4
votes

How would you display the contents of a QtSql table using a QTreeWidget? There isn't quite enough information around this particular subject...

1

1 Answers

3
votes

Your first stop should be the introduction to model/view programming in the QT documentation. Although in the QT documentation, it applies equally to PyQt and I had to get my ahead around this recently and found it to be by far the best place to start. From this, your SQL table is the model and your tree is the view.

Specifically, you probably want to use one of QSqlQueryModel, QSqlTableModel or QSqlRelationalTableModel as your model class and QTreeView (and not QTreeWidget as in your question) as the view. There is example code in all of those links on how to use the different classes.

But, it is unclear from your question how your table in SQL maps to a tree view - which makes providing any more detail impossible/accurate answer.

As an SQL table doesn't naturally map to a tree you might have to look at using a proxy model or extending QAbstractItemModel yourself to work with the QTreeView as this will be specific to your business logic (and whether you are using nested sets or materialised path etc). If you are not storing a tree in the database, then QListView or QTableView might be more appropriate view objects anyway.