Results 1 to 2 of 2

Thread: using QTreeView with a Database model

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Question using QTreeView with a Database model

    hello ppl,

    i'd like to visualize the contents of a small DB in a QTreeView.

    the data in the database looks like the following example:

    tbl_artist (ID INTEGER, NAME VARCHAR); ID is primary key
    tbl_cd (ID INTEGER, NAME VARCHAR, ID_ARTIST); ID is primary key, ID_ARTIST foreing key

    displaying this realation with a QSqlRelationalTableModel is pretty simple and would generally look like:
    Qt Code:
    1. //we're inside a QWidget or QDialog or etc.
    2. createDbConnection();
    3. relModel = new QSqlRelationalTableModel(this);
    4.  
    5. relModel->setTable("tbl_cd");
    6. relModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    7. relModel->setRelation(2, QSqlRelation("tbl_artist", "ID", "NAME"));
    8. model->select();
    9.  
    10. m_tableView->setModel(relModel);
    To copy to clipboard, switch view to plain text mode 

    i don't like the visualisation of the date inside a QTableView and would prefer the data to be arranged inside a QTreeview that would look like:

    |
    -artist1
    | |-cd1
    | |-cd2
    |
    -artist2
    | |-cd1

    ..... i hope you get what i would like...

    what i discovered so far is, that i have to use a QProxyModel to achieve it.
    i searched the net and diverse forums to get an idea of how to use the QProxyModel, but found nothing that helped me further

    i hope somebody here has the knowledge of how to link the 2 models.
    i'd appreciate any help or suggestions.

    greets
    darksaga

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: using QTreeView with a Database model

    You can start by taking a look at the transpose proxy model example in our wiki. It is very simple (just switches columns and rows), but should get you started. As I see from the db scheme you posted, you'll have to discard the artist column from the original model and instead implement a parent-child hierarchy.

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  3. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 17:28
  4. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53
  5. how to use QTreeView with Database model
    By mikro in forum Newbie
    Replies: 3
    Last Post: 13th April 2006, 16:12

Bookmarks

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.