Results 1 to 3 of 3

Thread: CheckBox inside a QTableView

  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default CheckBox inside a QTableView

    Hi,
    I try to have a checkBox inside a QtableView. For the moment, I try differents solutions (delegate, ..), but I'am not happy with the result. This is the code, I have for the moment
    Column 5 must be a checkBox
    class MyItemModel is derivated from QStandardItemModel

    Qt Code:
    1. bool MyItemModel::setData ( const QModelIndex & index, const QVariant & value, int role)
    2. {
    3. if (index.column()==5)
    4. {
    5. if (role==Qt::CheckStateRole)
    6. return QStandardItemModel::setData(index,value,role);
    7. return false;
    8. }
    9. else
    10. return QStandardItemModel::setData(index,value,role);
    11.  
    12. }
    13. QVariant MyItemModel::data ( const QModelIndex & index, int role ) const
    14. {
    15. if (index.column()==5)
    16. {
    17. if (role==Qt::CheckStateRole)
    18. return QStandardItemModel::data(index,role);
    19. return QVariant();
    20. }
    21. else
    22. return QStandardItemModel::data(index,role);
    23. }
    24. Qt::ItemFlags MyItemModel::flags( const QModelIndex & index ) const
    25. {
    26. if (index.column()==5)
    27. return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsUserCheckable;
    28. return Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable|Qt::ItemIsSelectable;
    29. }
    30.  
    31. When I add a line, I use this call
    32. setData(model->index(0, 5), Qt::Checked,Qt::CheckStateRole);
    To copy to clipboard, switch view to plain text mode 

    With that, I have a checkbox, but it's not centered and there is a text zone (I can edit it, if I put the flag Qt::ItemIsEditable). I woul like to remove the edit zone and center the check box.
    Thank for your help
    Laurent

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: CheckBox inside a QTableView

    You have to use custom delegate.
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Mar 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: CheckBox inside a QTableView

    Quote Originally Posted by axeljaeger View Post
    You have to use custom delegate.
    Hi,

    I have tried using custom delegates and it works fine.

    But i have another problem. If sorting is enabled in QTableView and i change the sort order then the delegate disappears i.e. it is not used after the model is sorted.

    Can some one suggest a way to enable sorting while using custom delegates.

    Another approach which i will be trying is to use the QHeaderView signal to indicate that sorting order has been changed and then call QTableView::sortByColumn


    thanks,

    Anuj

Similar Threads

  1. Replies: 1
    Last Post: 9th August 2009, 15:27
  2. Replies: 1
    Last Post: 6th July 2009, 01:53
  3. Replies: 2
    Last Post: 5th June 2008, 21:32
  4. QTableView with a checkbox
    By maxpower in forum Qt Programming
    Replies: 17
    Last Post: 18th February 2007, 09:45
  5. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

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.