PDA

View Full Version : using QTreeView with a Database model



darksaga
17th January 2007, 23:01
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:


//we're inside a QWidget or QDialog or etc.
createDbConnection();
relModel = new QSqlRelationalTableModel(this);

relModel->setTable("tbl_cd");
relModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
relModel->setRelation(2, QSqlRelation("tbl_artist", "ID", "NAME"));
model->select();

m_tableView->setModel(relModel);


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

wysota
17th January 2007, 23:29
You can start by taking a look at the transpose proxy model (http://wiki.qtcentre.org/index.php?title=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.