Results 1 to 3 of 3

Thread: PyQt4 - Populate QTreeView with data from database

  1. #1
    Join Date
    Feb 2015
    Qt products

    Default PyQt4 - Populate QTreeView with data from database

    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
    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”)
    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!!!!!

  2. #2
    Join Date
    May 2012
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: PyQt4 - Populate QTreeView with data from database

    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

    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.
    Last edited by StrikeByte; 27th February 2015 at 14:42.

  3. #3
    Join Date
    Feb 2015
    Qt products

    Default Re: PyQt4 - Populate QTreeView with data from database

    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 :
    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 ,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?

Similar Threads

  1. Populate QTreeView from database
    By yagabey in forum Qt Programming
    Replies: 15
    Last Post: 26th March 2015, 03:10
  2. Replies: 6
    Last Post: 20th February 2012, 11:59
  3. how to populate data in QTableWidget
    By gauravg in forum Qt-based Software
    Replies: 1
    Last Post: 25th March 2011, 13:50
  4. Replies: 3
    Last Post: 1st February 2011, 12:57
  5. How to populate delegate with model data.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 10:31

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.