Results 1 to 4 of 4

Thread: 2 Views 1 Model

  1. #1
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 2 Views 1 Model

    I have two QTreeViews using the same model (this works just fine).

    The TreeViews have a tree structure where nodes can be checked (clicked with a checkbox).

    I want one view to allow the user to change which nodes are clicked, and the other to not allow them to change them. I can't figure out how to do this. Setting one TreeView to be disabled isn't what I want because then they can't traverse the tree (which I still want). I just want it so that the user can't deselect a node in one view. Is there some way I can intercept all user interaction on the TreeView and only allow node traversal, ignoring the check box clicks?

    Thanks

  2. #2
    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: 2 Views 1 Model

    You can write a proxy model, that will alter the value returned by flags().

  3. #3
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: 2 Views 1 Model

    Yeah I have a model implementation, but I'm not sure how that helps since there will be instances where both views will be displayed at the same time, one editable and the other not allowing the check box clicks. How would I differentiate in the model between the 2 views?

    I have something like this in the flags method of the model:

    Qt Code:
    1. if (index.column() == 0)
    2. {
    3. //allow checkbox
    4. return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable |
    5. Qt::ItemIsTristate;
    6. }
    7. else
    8. {
    9. //don't allow check box
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: 2 Views 1 Model

    Quote Originally Posted by PrimeCP View Post
    How would I differentiate in the model between the 2 views?
    You will have two models --- original one and a proxy model.

    Qt Code:
    1. class ProxyModel : public QSortFilterProxyModel
    2. {
    3. Q_OBJECT
    4. public:
    5. ...
    6. virtual Qt::ItemFlags flags( const QModelIndex & index ) const
    7. {
    8. Qt::ItemFlags f = QSortFilterProxyModel::flags( index );
    9. if( _checkingDisabled ) {
    10. f &= ~ Qt::ItemIsUserCheckable;
    11. }
    12. return f;
    13. }
    14. ...
    15. };
    16.  
    17. ...
    18.  
    19. ProxyModel * proxyModel = new ProxyModel();
    20. proxyModel->setSourceModel( originalModel );
    21.  
    22. firstView->setModel( originalModel );
    23. secondView->setModel( proxyModel );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Drag and drop between model views
    By larry104 in forum Qt Programming
    Replies: 20
    Last Post: 19th January 2008, 16:09
  3. Replies: 4
    Last Post: 20th September 2007, 13:11
  4. Logging to a file and using same model for diff views
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 10th May 2007, 23:16
  5. Model Views and QLabel
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2006, 15:14

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.