Results 1 to 5 of 5

Thread: Checkbox in QTreeView

  1. #1
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Checkbox in QTreeView

    Hi,
    I need to add checkboxes to the first three columns in a QTreeView which gets its data
    from a QAbstractItemModel .
    Could some one tell me how to complete my model, so that the user can freely check and uncheck theses checkboxes.


    Qt Code:
    1. QModelIndex OutputsListModel::index ( int row, int column, const QModelIndex & parent) const
    2. {
    3. return createIndex(row, column);
    4. }
    5. QModelIndex OutputsListModel::parent ( const QModelIndex & index ) const
    6. {
    7. return QModelIndex();
    8. }
    9.  
    10. int OutputsListModel::rowCount ( const QModelIndex & parent) const
    11. {
    12. return(r);
    13. }
    14. int OutputsListModel::columnCount ( const QModelIndex & parent) const
    15. {
    16. return(c);
    17. }
    18. bool OutputsListModel::hasChildren ( const QModelIndex & parent ) const
    19. {.....
    20. }
    21. QVariant OutputsListModel::data ( const QModelIndex & index, int role) const
    22. { int i = index.column();
    23. if(role == Qt::DisplayRole)
    24. {.....
    25. }
    26. }
    27. Qt::ItemFlags OutputsListModel::flags(const QModelIndex& index) const
    28. {
    29. if (i == 0 || i== 1 || i == 2)
    30. return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    31. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    32. }
    33. bool OutputsListModel::setData(const QModelIndex& index, const QVariant& value, int role)
    34. {
    35. return true;
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mugi; 9th July 2011 at 00:15.

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Checkbox in QTreeView

    Use QStyledItemDelegate or QAbstractItemDelegate and in paint() paint QCheckbox (), i.e.
    Qt Code:
    1. QApplication::style()->drawControl( QStyle::CE_CheckBox, &cb_Style, painter );
    To copy to clipboard, switch view to plain text mode 
    then create editor QCheckBox if You want save data into model.

    Or simply in delegate paint() paint unchecked or checked box depending on the data, and change data not in delegate editor but in QTreeView signal clicked(QModelIndex). Connect that signal to slot and then use
    Qt Code:
    1. int data = model->data(index, Qt::DisplayRole).toInt();
    2. if( data == 0 )
    3. data = 1;
    4. else
    5. data = 0;
    6.  
    7. model->setData(data, index );
    To copy to clipboard, switch view to plain text mode 
    and delegate will autoamatically update view. So that way You don't create editor.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Checkbox in QTreeView

    thanks a lot for the quick reply
    If I don't want to use delegates (actually I don't know how to ), is there another way to update my view using just my QAbstractItemModel.
    Judging from this post it should be possible, the thing is I don't know how to complete the Data and SetData Methods

    http://www.qtcentre.org/threads/4213...kable-checkbox
    Last edited by mugi; 9th July 2011 at 00:31.

  4. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Checkbox in QTreeView

    IMHO the 2nd way, without editor in delegate is the way You should go.

    This will work like this
    QTreeView part:
    -> user click on index in QTreeView, change model data (signal click(QModelIndex)) - toggle currently stored value from 0 to 1 and from 1 to 0

    QAbstractItemDelegate part:
    -> paint() paint's checkbox and use that int value to determine what state it should has. setEditorData() (<- You don't need this one when You don't createEditor()) taht way code is simpler for this purpouse IMHO

    Model part:
    -> setData(), data()

    I don't want write here code for You, but what can help You are examples, especially:
    model = qtdir/examples/itemviews/editabletreemodel
    delegate = qtdir/examples/itemviews/coloreditorfactory (use checkBox, example uses combobox)
    Last edited by Talei; 9th July 2011 at 01:00. Reason: updated contents
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  5. #5
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Checkbox in QTreeView

    thanks
    I will give it a shot

Similar Threads

  1. Replies: 3
    Last Post: 1st April 2011, 05:58
  2. Replies: 0
    Last Post: 5th July 2010, 10:05
  3. QTreeView, checkbox problem
    By jwieland in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2009, 15:09
  4. How to set StyleSheet for checkbox in QTreeView;
    By visor_ua in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2009, 12:46
  5. QTreeView: How to center a checkbox in a cell
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 13:11

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.