Results 1 to 12 of 12

Thread: My first model/view attempt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default My first model/view attempt

    I wish to create something vith a treeview. The top items should be of class A, and second level of class B. I will have a button to add items of class A, and I think that when I right click on an Item A, I will be able to add a B object to that branch.

    What classes would you recomend me using for this?

    Btw. Is there any pdf version of the docs at docs.trolltech.com? I prefer reading their model/view docs on paper and printing them directly from webpages doesn't look so very nice.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first model/view attempt

    See QTreeWidget, QTreeWidgetItem and QTreeWidget::addTopLevelItem and QTreeWidgetItem::addChild (for adding B objects via A context menu).
    And no, t
    here is no pdf version.

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first model/view attempt

    I see that there is both QTreeWidget and QTreeView. Item based versus Model based. Which one should you use in what sitution?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first model/view attempt

    QTreeWidget is derived from QTreeView, so under the hood it is still model based.
    You should use QTreeView when you want more control over the model and/or item delegate.

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first model/view attempt

    Quote Originally Posted by marcel View Post
    QTreeWidget is derived from QTreeView, so under the hood it is still model based.
    It's still model based that's for sure but the interaction with the model is wicked... It is practically impossible to play with model, programmatically I mean, without using the item based approach (which soon does not fit if you want to have some control over your data...)

    Quote Originally Posted by marcel View Post
    You should use QTreeView when you want more control over the model and/or item delegate.
    QTreeView allows you to use any kind of data as a "source" for the model. Views interact with the model data through QModelIndex, delivered by the model, and used to query data from it later on. This approach would easily allow you to have your objects of class A and B stored somewhere (accessible to the model) while using them to show some content within a tree view. all you'd need is subclassing QAbstractItemModel according to the guidelines and this is easy enough.
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first model/view attempt

    I'm reading this, but exactly how do I glue everything together with the data? Should I store all the data in object A inside the customized modell or store it elsewhere and just keep a pointer in the modell?

    There are more stuff in the program that should interct with the modell.
    Well, I don't blame you if you do not understand this question. I sure don't.
    I guess I have to go back to reading.

  7. #7
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first model/view attempt

    What I meant was, when I click on something in the view, I want the object that this view corresponds to in the model, to send signals to update other widgets. How do you do this in a nice clean way?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: My first model/view attempt

    Perhaps this wiki article helps: QAbstractItemModel
    J-P Nurmi

  9. #9
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: My first model/view attempt

    Well, I have written some code now.
    But I still think about the structure. I will have a single top level object of class Solution, and this one will have a lot of children of type Table. I'm not really clear on how good it will be, but time will tell.

    Anyway, I think I added a new Solution object (by using the File menu) but nothing is displayed.
    Any hits would be very helpful.
    Code here http://taljaren.se/kod.tgz

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

    Default Re: My first model/view attempt

    Maybe it's not too late to add my five cents - I suggest you start with QStandardItemModel, maybe it fits your needs and you won't need to implement your own model.

  11. #11
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first model/view attempt

    Your cents are always very valuable so it's not to late.
    I'll have a look at the standard item.
    Do you have any cents on how you put different objects in the tree?
    It could look like this
    Qt Code:
    1. A
    2. |
    3. +-B
    4. +-D
    5. |
    6. +-D
    7. +-B
    8. +-D
    9. |
    10. +-E
    11. +-B
    12. |
    13. +-C
    14. +-F
    15. |
    16. +-F
    17. +-C
    18. +-F
    To copy to clipboard, switch view to plain text mode 
    Should I put generic elements like this
    Node
    {
    int type; // A,B,C,D,E,
    baseclass* obj; // pointer to either A,B,C,D,E,F object
    }

    Or how do you do this?

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

    Default Re: My first model/view attempt

    It depends whether you want to implement a model from scratch or use one of the already existing. For the former you can do what you want. For the latter I suggest you just use QStandardItem objects or similar. How much data do you want to store in each node and do you already have some representation of the data tree anywhere?

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 20:36
  2. model/view, can't hide row/col
    By grellsworth in forum Qt Programming
    Replies: 7
    Last Post: 17th September 2007, 14:21
  3. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  4. MODEL/VIEW programming and TABLES
    By newbie in forum Qt Programming
    Replies: 5
    Last Post: 27th August 2006, 21:26
  5. MODEL/VIEW programming
    By mira in forum Newbie
    Replies: 3
    Last Post: 21st April 2006, 11:19

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
  •  
Qt is a trademark of The Qt Company.