Results 1 to 10 of 10

Thread: Sql Query with QTreeView - Advice

  1. #1
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Sql Query with QTreeView - Advice

    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
    Last edited by johnnyturbo3; 7th September 2010 at 15:32.

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sql Query with QTreeView - Advice

    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?

  3. #3
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Sql Query with QTreeView - Advice

    The QSqlQueryModel will only fetch data in an unstructured form. So, when you:
    Qt Code:
    1. treeView->setModel(sqlQueryModel);
    To copy to clipboard, switch view to plain text mode 
    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):

    Qt Code:
    1. void GUI::createModel(){
    2. model1 = new QStandardItemModel(5, 2); //5 rows, 2 columns
    3. for(int r = 0; r < 5; r ++ ){
    4. QStandardItem* item = new QStandardItem(QString("File%0").arg(r)); //Top level
    5. if(c == 0){
    6. for(int i = 0; i < 3; i ++ ){
    7. QString("Item %0").arg(i));
    8. QString("Item %0").arg(i));
    9. child -> appendRow(child1);
    10. item->appendRow(child);
    11. }
    12. }
    13. model1->setItem(r, c, item);
    14. }
    15. model1->setHorizontalHeaderItem(0, new QStandardItem("Name"));
    16. model1->setHorizontalHeaderItem(1, new QStandardItem("Value"));
    17. }
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sql Query with QTreeView - Advice

    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?

  5. #5
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Sql Query with QTreeView - Advice

    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.

  6. #6
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sql Query with QTreeView - Advice

    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.

  7. The following user says thank you to waynew for this useful post:

    johnnyturbo3 (13th September 2010)

  8. #7
    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: Sql Query with QTreeView - Advice

    Quote Originally Posted by johnnyturbo3 View Post
    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    johnnyturbo3 (13th September 2010)

  10. #8
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Sql Query with QTreeView - Advice

    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.

  11. #9
    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: Sql Query with QTreeView - Advice

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Sql Query with QTreeView - Advice

    Just to clarify, this is the example you're on about:
    http://www.qtcentre.org/wiki/index.p...se_Proxy_Model

Similar Threads

  1. Advice for an Application
    By salmanmanekia in forum Newbie
    Replies: 1
    Last Post: 19th April 2010, 12:06
  2. QtConcurrent, i need advice
    By SABROG in forum Qt Programming
    Replies: 10
    Last Post: 29th December 2009, 19:53
  3. Replies: 2
    Last Post: 27th August 2009, 20:31
  4. Need Advice: Best IDE for Mac OSX
    By JimDaniel in forum Qt Programming
    Replies: 6
    Last Post: 18th October 2008, 23:14
  5. Newbie needs advice
    By Seth in forum Newbie
    Replies: 3
    Last Post: 12th April 2007, 22:50

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.