Results 1 to 19 of 19

Thread: QSql*Model + QTreeView : make a tree

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2007
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question QSql*Model + QTreeView : make a tree

    Hello,

    I would like to have the data contained in an SQL table shown in a treeview (I already have that), but I would like items to be group (kind of filter) by types. See the exemple.

    Here is my table "MultimediaFiles" structure:
    - id,
    - name,
    - path,
    - type (can be, "picture", "video", "sound").

    So, I would like my records to be presented in a treeview like this :

    |-- Name --|-- Path --|
    ------------------------------------
    - picture
    |- foo.png C:\...
    - video
    |- bar.avi C:\...
    |- toto.mpg C:\...
    - sound
    |- huhu.wav ...
    |- haha.mp3 C:\...

    What I already did :

    Qt Code:
    1. SQL::SQL()
    2. {
    3. QSqlDatabase mDb = QSqlDatabase::addDatabase( "QMYSQL" );
    4. mDb.setHostName( "localhost" );
    5. mDb.setDatabaseName( "mydb" );
    6. mDb.open();
    7. setupModel();
    8. }
    9.  
    10. void SQL::setupModel()
    11. {
    12. pModel = new QSqlRelationalTableModel();
    13. pModel->setTable( "MultimediaFiles" );
    14. if( !pModel->select() )
    15. {
    16. qDebug() << "pb select";
    17. qDebug() << pModel->lastError().text();
    18. }
    19.  
    20. treeView->setModel( pModel );
    21. /*...*/
    22. treeView->show();
    23. }
    To copy to clipboard, switch view to plain text mode 

    But it doesn't act as I want : there is no node created.
    So, do I miss something, or do I need to make my own model, or add treeItems by hands ?



    Thanks in advance.

  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: QSql*Model + QTreeView : make a tree

    The model you used is a flat model and no matter what view you use it won't become structured just by itself. You need to provide a proxy model that will represent your table as a tree. See QAbstractProxyModel for details.

  3. #3
    Join Date
    Oct 2007
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSql*Model + QTreeView : make a tree

    Great, I take a look.

    Thank you.

  4. #4
    Join Date
    Oct 2007
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSql*Model + QTreeView : make a tree

    I'm back. I'm totally lost :/

    I understood I have to subclass QAbstractProxyModel, but I really don't know what to write in the methods.
    Could you please help me a little bit ?

  5. #5
    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: QSql*Model + QTreeView : make a tree

    The proxy is to transform a flat model into a tree. You need to return proper model indexes and parent indexes. If you don't feel comfortable enough with implementing your own proxy model (which is not very simple) I suggest you use QStandardItemModel and manually fill it with data fetched from the sql table.

  6. #6
    Join Date
    Oct 2007
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSql*Model + QTreeView : make a tree

    Hello,

    Well, I want to learn, so I will try to make a proxy.

    Tell me if I'm wrong :
    - I create a TreeModel class (based on the Simple Tree model example from doc.trolltech.com)
    - Every time my Qsql*Model will change, I will populate my TreeModel through the Proxy.
    - The proxy will make the correspondance between the Qsl*Model and my TreeModel.

    Does it sound good ?

  7. #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: QSql*Model + QTreeView : make a tree

    No. You need to subclass QAbstractProxyModel.

  8. #8
    Join Date
    Oct 2006
    Posts
    12
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql*Model + QTreeView : make a tree

    ProxyModel is deprecated... You should take a look at simple tree view example. You can make a tree model from flat sql result by choosing what columns will be the tree parent creating initiator (I'm bad with english )

    For example:

    Qt Code:
    1. Column_001 | Column_002
    2. -----------+-----------
    3. value_001 | value002
    4. value_001 | value003
    5. value_002 | value004
    6. value_002 | value005
    7. value_003 | value006
    To copy to clipboard, switch view to plain text mode 

    You should get (with the use of some code logic):

    Qt Code:
    1. value_001
    2. +-- value_002
    3. +-- value_003
    4. value_002
    5. +-- value_004
    6. +-- value_005
    7. value_003
    8. +-- value_006
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSql*Model + QTreeView : make a tree

    I build mysql netset on a tree ... to infinit loop whithout proxie or so..
    The query is long but running...

    #define TREECAT "SELECT n.*, round((n.rgt-n.lft-1)/2,0) AS childs, count(*)+(n.lft>1) AS level, ((min(p.rgt)-n.rgt-(n.lft>1))/2) > 0 AS lower, (( (n.lft-max(p.lft)>1) )) AS upper FROM FAKTURA_SUB n, FAKTURA_SUB p WHERE n.lft BETWEEN p.lft AND p.rgt AND (p.root_id = n.root_id) AND (p.id != n.id OR n.lft = 1) GROUP BY n.root_id,n.id ORDER BY n.root_id,n.lft"

    You running only level 1 rows and append to QStandardItemModel(); root
    all sub level go to self on composing ....
    and you can move level up or dow append ....

    a tree online demo http://www.klempert.de/nested_sets/demo/



    Qt Code:
    1. QList<QStandardItem *> Cat_Tree::Compose( int rowposition )
    2. {
    3. QSqlRecord r = ReadModel->record(rowposition);
    4. int level;
    5. int childs;
    6. QList<QStandardItem *> Horrizzontal_Line;
    7. for (int x = 0; x < DisplayFields.size(); ++x) {
    8.  
    9. Horrizzontal_Line.append( xxxxx )
    10. }
    11. /* check on qmap unique ! */
    12. if (!OnBox(rowposition)) {
    13. RowUnique.insert(rowposition,level);
    14. }
    15. /* if moore childs append ..... loops */
    16. if (childs > 0 ) {
    17. for (int i = 0; i < childs; ++i) {
    18. const int jumpto = i + 1 + rowposition;
    19. if (!OnBox(jumpto)) {
    20. diritto.first()->appendRow(Compose(jumpto));
    21. }
    22. }
    23. }
    24. return diritto;
    25. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  10. #10
    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: QSql*Model + QTreeView : make a tree

    Quote Originally Posted by vladeck View Post
    ProxyModel is deprecated...
    Oh, really? Very interesting... since when?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql*Model + QTreeView : make a tree

    Quote Originally Posted by wysota View Post
    Oh, really? Very interesting... since when?
    4.1 or something. Just when QAbstractProxyModel and QSortFilterProxyModel came in to replace it.

  12. #12
    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: QSql*Model + QTreeView : make a tree

    Quote Originally Posted by jacek View Post
    4.1 or something. Just when QAbstractProxyModel and QSortFilterProxyModel came in to replace it.
    Gee... isn't that QAbstractProxyModel I was writing about? I could have sworn the "abstract" word was there... and it still is, isn't it?

Similar Threads

  1. QTreeView with QStandardItemModel
    By steg90 in forum Newbie
    Replies: 3
    Last Post: 16th May 2007, 09:28
  2. Replies: 3
    Last Post: 19th April 2007, 11:42
  3. Window OS make distclean && qmake && make one line
    By patrik08 in forum General Programming
    Replies: 4
    Last Post: 22nd March 2007, 10:43
  4. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57
  5. Optimizing filterAcceptsRow() to filter a tree
    By vfernandez in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2007, 12: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.