Results 1 to 6 of 6

Thread: Checkboxes in QTableView with my custom model

  1. #1
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Checkboxes in QTableView with my custom model

    I read a few threads on this subject but stayed kind of confused.

    I have a custom model and a QTableView, and I'm using a custom class deriving from QItemDelegate for two columns I need widgets in (a QSpinBox like in the Qt example, and a QComboBox).

    Now I want to add a checkbox, but unlike the combo and spinbox, where the actual widget becomes displayed only in edit mode, I want my checkboxes to be always displayed. Of course I'll also want to connect to signals emitted from checking/unchecking.

    What's the best way to add these checkboxes in my new column?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Checkboxes in QTableView with my custom model

    QTableView can display a checkbox by default, just enable Qt::ItemIsUserCheckable flag in the model in QAbstractItemModel::flags(). For signals when checked, you need to explicitly emit signals (user defined signals) when user checks the item, i.e. from QAbstractItemModel::setData()

  3. The following user says thank you to Santosh Reddy for this useful post:

    frankiefrank (14th August 2011)

  4. #3
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Checkboxes in QTableView with my custom model

    You'll also need this in your data(const QModelIndex &index, int role) method:

    Qt Code:
    1. if (role == Qt::CheckStateRole) // this shows the checkbox
    2. {
    3. bool aBool = fieldValue;
    4. if (aBool)
    5. return Qt::Checked;
    6. else
    7. return Qt::Unchecked;
    8. }
    To copy to clipboard, switch view to plain text mode 

    jeff

  5. The following 2 users say thank you to Jeffb for this useful post:

    davidovv (1st January 2013), frankiefrank (14th August 2011)

  6. #4
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Checkboxes in QTableView with my custom model

    Thanks for the help. Still a bit confused about the data() / setData().

    My model's data() method for the
    Qt Code:
    1. Qt::CheckStateRole
    To copy to clipboard, switch view to plain text mode 
    should return the checked/unchecked status. But based on what do I decide that?
    Here's what I mean - I want the checkable status to "represent" a boolean value.
    Do I need to store the boolean value "behind the scenes" in a custom user role?


    Added after 1 47 minutes:


    Right now, I manage to get checkboxes displayed but I can't edit them (can't check).

    My flags() implementation adds the following flags to the checkbox column
    Qt Code:
    1. flags |= Qt::ItemIsUserCheckable;
    2. flags |= Qt::ItemIsSelectable;
    3. flags |= Qt::ItemIsEnabled;
    To copy to clipboard, switch view to plain text mode 

    My setData stores a string "true"/"false" in an underlying object representing the current state, only when called for the checkbox column and the check state role.

    I put a breakpoint in the setData code relevant to that column and role, but I don't see any calls. Clicks on the checkbox don't cause it to get there.
    Last edited by frankiefrank; 15th August 2011 at 15:43.

  7. #5
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Checkboxes in QTableView with my custom model

    Here is an example of the setData method in one of my models:

    Qt Code:
    1. bool JB_NodeModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. bool success = true;
    4.  
    5. if (index.isValid())
    6. {
    7. // get field name of index
    8. QString fieldName = mapped_dbFieldNames[index.column()];
    9. JB_Node* node = nodeFromIndex(index);
    10.  
    11. if (role == Qt::CheckStateRole)
    12. {
    13. // Columns with a checkbox must be included here so it can be edited through the checkbox on the QtableView
    14. if (editableCheckBoxDBFieldNamesList.contains(fieldName))
    15. {
    16. QVariant boolValue = QVariant(value.toBool());
    17. node->setData(fieldName, boolValue); // this is the important part
    18. QModelIndex topLeft = index;
    19. QModelIndex bottomRight = index;
    20. emit dataChanged(topLeft, bottomRight);
    21. success = true;
    22. }
    23. }
    24. else
    25. {
    26. node->setData(fieldName, value);
    27. QModelIndex topLeft = index;
    28. QModelIndex bottomRight = index;
    29. emit dataChanged(topLeft, bottomRight);
    30. success = true;
    31. }
    32. }
    33. else
    34. success = false;
    35.  
    36. return success;
    37. }
    To copy to clipboard, switch view to plain text mode 


    Added after 5 minutes:


    Also

    you only need
    Qt Code:
    1. flags |= Qt::ItemIsUserCheckable;
    To copy to clipboard, switch view to plain text mode 
    in your flags method to make your checkboxes checkable.

    if you don't want your checkbox columns/fields to be editable, then use
    Qt Code:
    1. flags |= Qt::ItemIsSelectable;
    2. flags |= Qt::ItemIsEnabled;
    To copy to clipboard, switch view to plain text mode 
    for those fields.
    Last edited by Jeffb; 17th August 2011 at 13:35.

  8. The following user says thank you to Jeffb for this useful post:

    frankiefrank (22nd August 2011)

  9. #6
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Checkboxes in QTableView with my custom model

    Quote Originally Posted by Santosh Reddy View Post
    QTableView can display a checkbox by default, just enable Qt::ItemIsUserCheckable flag in the model in QAbstractItemModel::flags(). For signals when checked, you need to explicitly emit signals (user defined signals) when user checks the item, i.e. from QAbstractItemModel::setData()
    I need the ability to show checkbox, I am still confuse on this issue. How do I set the flags. QAbstractItemModel::flags() is a getter not a setter, right?

Similar Threads

  1. Replies: 5
    Last Post: 12th April 2011, 10:03
  2. No checkboxes are displayed in a QTableView
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2010, 18:28
  3. Replies: 0
    Last Post: 1st February 2010, 12:00
  4. Unsolicited checkboxes in QTableView
    By MattPhillips in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2009, 22:44
  5. QTableView and checkboxes
    By ibergmark in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2008, 16:20

Tags for this Thread

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.