PDA

View Full Version : Sql Query with QTreeView - Advice



johnnyturbo3
7th September 2010, 15:27
Hi,

I currently have a program that use a QSqlQueryModel to populate a QTableView. I now want to change from QTableView to QTreeView.

I've done some searching for examples and quick fixes, but has left me more confused than anything. So, I understand that I have to build my own data structure using either QStandardItemModel or a proxymodel.

The query that is needed to populate the QTreeView is going to be very large.

Does anyone have advice on what route to take the achieve this?
Thanks

waynew
8th September 2010, 22:59
I'm hardly an expert, but since no one else is responding...

Why do you feel that you have to build your own data structure?
Why can't you use QTreeView just like you used QTableView?
What database are you using?

johnnyturbo3
9th September 2010, 08:48
The QSqlQueryModel will only fetch data in an unstructured form. So, when you:

treeView->setModel(sqlQueryModel);
You have all the values of the query displayed in an unstructured form.

There are a couple of suggestions on the web, and the easiest one seems to be to create a QStandardItemModel and create a hierarchy from the SQL query. I have a method at the moment (note: it won't compile and there are redundant variables in it at the moment):


void GUI::createModel(){
model1 = new QStandardItemModel(5, 2); //5 rows, 2 columns
for(int r = 0; r < 5; r ++ ){
QStandardItem* item = new QStandardItem(QString("File%0").arg(r)); //Top level
if(c == 0){
for(int i = 0; i < 3; i ++ ){
QStandardItem *child = new QStandardItem(
QString("Item %0").arg(i));
QStandardItem *child1 = new QStandardItem(
QString("Item %0").arg(i));
child -> appendRow(child1);
item->appendRow(child);
}
}
model1->setItem(r, c, item);
}
model1->setHorizontalHeaderItem(0, new QStandardItem("Name"));
model1->setHorizontalHeaderItem(1, new QStandardItem("Value"));
}

The database (SQLite) holds video data. I want the 'root' to be the file name, and the children to be the names of the tables the values are from.
The result I want is :
|+file1
||+video -Video table
|||--frameRate
|||--codec
||+audio -audio table
|||-bitRate
|||-codec
|+file2
||+video -Video table
|||--frameRate
|||--codec
||+audio -audio table
|||-bitRate
|||-codec

So, I would be grateful if someone could guide me in a painless way of achieving this. I hope I've made myself clear.

waynew
9th September 2010, 11:33
In order to explore the options, more input is needed.
What database are you using?
What is the source of your data?
What is the structure of the data in the source?
Are you going to do a one time load of the model from the source, then add data with a gui, or add data with the load code?

johnnyturbo3
9th September 2010, 14:03
What database are you using?
- SQLite


What is the source of your data?
- Not sure what this means. The data is already in the database. The application allows the user to select multiple criteria, for which a SQL query is made up of these criteria. The results are displayed in a Tree View.


What is the structure of the data in the source?
- There are five tables. Each of these tables holds information about a video file. Fileproperties, videoInfo, audioInfo, transportStream and singalRoot. Fileproperties has a 1:Many relationship with the other 4 tables.


Are you going to do a one time load of the model from the source, then add data with a gui, or add data with the load code?
- When a criteria is selected, eg videocodec = 'Mpeg', then the results of the table are updated in real-time. The items in the Tree View are reflected by what options the user chooses from the dropdown box.

There is no updating or altering of the DB involved.

waynew
9th September 2010, 23:32
Ok, that helps. I see your problem now.
You say the values are displayed in an unstructured form. One would expect the model data to follow the query structure.
Sounds like that is not happening.

wysota
10th September 2010, 02:51
So, I would be grateful if someone could guide me in a painless way of achieving this.
Maybe not a painless (rather quite painful) but correct method to do this would be to implement a proxy model that would transform your flat table into a tree. It's not really that difficult. But if you want to avoid it then using QStandardItemModel would indeed be the fastest way (until you come up with an idea that you'd like to modify the data).

johnnyturbo3
13th September 2010, 09:06
I guess I'll go down the proxy model route.
I'm currently looking for some sample code on the net. Do you know of any sample code that might help me? I quickly lose faith when trying to use just the official class references.

Thanks for your help.

wysota
13th September 2010, 09:14
There is an example of a proxy model in our wiki, not the best one but should help you get started. Transforming a flat model to a tree is quite easy, much easier than going the other way.

johnnyturbo3
13th September 2010, 10:10
Just to clarify, this is the example you're on about:
http://www.qtcentre.org/wiki/index.php?title=Transpose_Proxy_Model