PDA

View Full Version : Qtreeview from database(sqlite)



cengiz3519
4th June 2014, 14:23
hi I am new user qt5
(I am .net csharp developer)

I cant find example; Qtreeview(Qtreewidgets) fill from database.

Is there any one know; where can I find this example.

my database table like this

ID; ParentID; Name; Category

d_stranz
5th June 2014, 00:42
SQL tables are usually placed in a QSqlTableModel and displayed in a QTableView. If you want your table displayed as a tree instead, then you will have to create a custom QAbstractItemModel around your SQL data and implement the index(), parent(), data(), rowCount(), and columnCount() methods correctly for it.

The easiest way to do this is to create a C++ - based tree for your SQL results. You will have to build this tree manually by inserting each result row at the proper place in the tree based on the ID / ParentID relationships. Then, for each QModelIndex you create in your index() method, you set the QModelIndex::internalPointer() to the node in your C++ tree where the row can be found.

You can't use QSqlTableModel or any of the related models because they all are based on the assumption that the data is a table, not a tree. The difference between a tree and a table is that for a table, the parent() method always returns an invalid QModelIndex. For a tree, only the top-most node returns an invalid index.

For examples, try this link (http://www.lmgtfy.com/?q=Qt+tree+model).