View Full Version : PyQt4 - Populate QTreeView with data from database

27th February 2015, 09:12
Hello everybody!!I am quite new to PyQt and programming.I am trying to populate a QTreeView from an sqlite3 database ,but with no success. My database has this form:
column1 | columnn2
sth1\sth2\sth3 |
sth4\sth5 | value00
sth6\sth7\sth8 |
sth9\sth10\sth11\sth12 | value01
sth9\sth10\st11\sth13 | value02

The QTreeView will have two columns and will look like this:

|-----sth5 |---value00
|-----sth12 |----value01
|-----sth13 |----value02

I searched the internet and I found that I must use QSqlDatabase,QSqlTableModel and QSqlQuery.
I created the database inside my gui code using these :
db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
if not db.open():
QtGui.QMessageBox.warning(None,"My Program", QString("Database Error: %1").arg(db.lastError().text()))
return False

query.exec_('CREATE TABLE IF NOT EXISTS mytable (mypaths TEXT , myvalues TEXT)')
and this:
query.exec_("INSERT INTO mytable VALUES(?,?)", (…,…))

I read some of Summerfield 's chapters and I know how to create the QTreeView and that as a model I have to use QSqlTableModel:
self. model=QSqlTableModel(self)
self. model.setTable(“mytable”)
self. model.select()
but I have absolutely no idea what to do next. I don’t know how to put the database’s data into the QTreeView!
I would really appreciate any king of help!!!!Thanks in advance!!!!!

27th February 2015, 13:26
For this situation there is not out of the box solution you will have to write a custom TreeModel. you can find information about it here http://qt-project.org/doc/qt-4.8/itemviews-simpletreemodel.html

For a system like this, I would use a JSon or XML file or a NoSql database. SQL databases arn't realy fit for storing tree information but here are some ways to do it in a SQL database, each approach has its drawbacks.
There are several options possible
1) a single data table with a link table for parrent child relations (with this solution you can make a tree with multiple levels):

Table 1 (data table):

Table 2 (parenting link table)
id|parent id|child id

both the parent and the child id are a Id from the data table (foreign key), there are some problems with this approach a child can have multiple parents and a parent can also be it's own child

2) a single table solution (multiple levels are possible)
Table 1 (data table with a id linking it to its parent)
id|name|value|parent id

The parent id points to a Id in the same table the top item should not have a parent id.

3) a table for parrent items and a table for child items (this solution can only have one level)
Table 1 (parent table)

Table 2 (child table)
id|name|value|parent id

the parent id points to the id of the parent table (foreign key)

4) a table that stores a JSon or XML file for each top item

this way you only have a name for the top item, all the sub items are stored in the blobFile.

26th March 2015, 01:56
Thanks,though I have already written the database...and sorry for bothering you again but I am quite confused about some things in QTreeView and QSqlQuery.For example,how do I load the database???I found this one on the internet : http://www.qtforum.org/article/33291/qsqlquerymodel-and-qtreeview.html
and although it's in C++ (I think),I guess that it's done the same way in Python(besides the syntax). How does he load the database in this if there is already a database???And the query.next() ,how exactly does it work??For instance,does it read each row at a time if we have multiple columns or each cell at a time?