Results 1 to 6 of 6

Thread: Checkbox in QListView

  1. #1
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Checkbox in QListView

    I've looked at all the posts on this topic and I just cannot find a solution for this problem.

    I have a QListView populated with a QSqlQueryModel and I need to add a checkbox to the list the my code displays all the records, but refuses to display the checkbox:

    Qt Code:
    1. selectionSetModel *selectionViewModel = new selectionSetModel();
    2.  
    3. selectionViewModel->setQuery("select selected, selectionset, filecount, size from selectionsets");
    4. selectionViewModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Selected"));
    5. selectionViewModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Selection Set"));
    6. selectionViewModel->setHeaderData(2, Qt::Horizontal, QObject::tr("File Count"));
    7. selectionViewModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Size"));
    8.  
    9. ui->selectionsetTableView->setModel(selectionViewModel);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtSql>
    2. #include <QTextCharFormat>
    3. #include <QtDebug>
    4.  
    5. #include "includes/selectionsetmodel.h"
    6.  
    7.  
    8. selectionSetModel::selectionSetModel(QObject *parent) : QSqlQueryModel(parent)
    9. {
    10. }
    11.  
    12. Qt::ItemFlags selectionSetModel::flags(const QModelIndex &index) const
    13. {
    14. if(index.column()==0)
    15. {
    16. Qt::ItemFlags flags;
    17. flags |= Qt::ItemIsSelectable;
    18. flags |= Qt::ItemIsEditable;
    19. flags |= Qt::ItemIsEnabled;
    20. flags |= Qt::ItemIsUserCheckable;
    21. return flags;
    22. }
    23. else
    24. {
    25. return QAbstractItemModel::flags(index);
    26. }
    27. }
    28.  
    29. QVariant selectionSetModel::data(const QModelIndex &index, int role)
    30. {
    31.  
    32. QVariant value = QSqlQueryModel::data(index, role);
    33. if (index.column() == 0)
    34. {
    35. if ( index.column() == 0 )
    36. {
    37. // Change this to suit your particular model storage backend
    38. qDebug() << "Returned checked";
    39.  
    40. return Qt::Checked;
    41. }
    42. }
    43. else
    44. {
    45. return value;
    46. }
    47. }
    48.  
    49. bool selectionSetModel::setData(const QModelIndex &index, const QVariant &value, int role )
    50. {
    51.  
    52. QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
    53. QString id = data(primaryKeyIndex, role).toString();
    54.  
    55. clear();
    56.  
    57. bool ok;
    58. if (index.column() == 0) {
    59. ok = setSelected(id, value.toInt());
    60. }
    61. refresh();
    62. return ok;
    63. }
    64. //! [1]
    65.  
    66. void selectionSetModel::refresh()
    67. {
    68. setQuery("select selected, selectionset, filecount, size from selectionsets");
    69. setHeaderData(0, Qt::Horizontal, QObject::tr("Selected"),Qt::CheckStateRole);
    70. setHeaderData(1, Qt::Horizontal, QObject::tr("Selection Set"));
    71. setHeaderData(2, Qt::Horizontal, QObject::tr("File Count"));
    72. setHeaderData(2, Qt::Horizontal, QObject::tr("Size"));
    73. }
    74.  
    75. //! [2]
    76. bool selectionSetModel::setSelected(QString id, qint8 selected)
    77. {
    78. QSqlQuery query;
    79. query.prepare("update selectionsets set id = ? where selected = ?");
    80. query.addBindValue(id);
    81. query.addBindValue(selected);
    82. return query.exec();
    83. }
    84. //! [2]
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef SELECTIONSETMODEL_H
    2. #define SELECTIONSETMODEL_H
    3.  
    4. #include <QSqlQueryModel>
    5.  
    6.  
    7.  
    8. class selectionSetModel : public QSqlQueryModel
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. selectionSetModel(QObject *parent = 0);
    14.  
    15. Qt::ItemFlags flags(const QModelIndex &index) const;
    16. bool setData (const QModelIndex &index, const QVariant &value, int role);
    17. QVariant data(const QModelIndex &index, int role);
    18.  
    19. private:
    20. bool setSelected(QString id, qint8 selected);
    21. void refresh();
    22. };
    23.  
    24.  
    25. #endif // SELECTIONSETMODEL_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by silmaril; 26th January 2014 at 06:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checkbox in QListView

    Your data() method needs to answer the request for role Qt::CheckStateRole for whatever column you want to have the checkbox displayed in.

    Let assume column 0

    Qt Code:
    1. QVariant selectionSetModel::data(const QModelIndex &index, int role)
    2. {
    3. if (index.column() == 0) {
    4. if (role == Qt::CheckStateRole) {
    5. QVariant selected = QSqlQueryModel::data(index, Qt::EditRole); // or Qt::UserRole
    6. return selected.toInt() != 0; // assuming the value is 0 if not selected and != 0 if selected
    7. }
    8. return QVariant(); // no other data for this column
    9. }
    10. return QSqlQueryModel::data(index, role); // default behavior for other columns
    11. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Checkbox in QListView

    Thank you for your reply.

    I applied the code you supplied and it still does not work.

    I can see the int value in the list but it's not rendering the checkbox.

    Qt Code:
    1. #include <QtSql>
    2. #include <QTextCharFormat>
    3. #include <QtDebug>
    4.  
    5. #include "includes/selectionsetmodel.h"
    6.  
    7.  
    8. selectionSetModel::selectionSetModel(QObject *parent) : QSqlQueryModel(parent)
    9. {
    10. }
    11.  
    12. Qt::ItemFlags selectionSetModel::flags(const QModelIndex &index) const
    13. {
    14. if(index.column()==0)
    15. {
    16. Qt::ItemFlags flags;
    17. flags |= Qt::ItemIsSelectable;
    18. flags |= Qt::ItemIsEditable;
    19. flags |= Qt::ItemIsEnabled;
    20. flags |= Qt::ItemIsUserCheckable;
    21. return flags;
    22. }
    23. else
    24. {
    25. return QAbstractItemModel::flags(index);
    26. }
    27. }
    28.  
    29. QVariant selectionSetModel::data(const QModelIndex &index, int role)
    30. {
    31. if (index.column() == 0) {
    32. if (role == Qt::CheckStateRole) {
    33. QVariant selected = QSqlQueryModel::data(index, Qt::EditRole); // or Qt::UserRole
    34. return selected.toInt() != 0; // assuming the value is 0 if not selected and != 0 if selected
    35. }
    36. return QVariant(); // no other data for this column
    37. }
    38. return QSqlQueryModel::data(index, role); // default behavior for other columns
    39. }
    40.  
    41. bool selectionSetModel::setData(const QModelIndex &index, const QVariant &value, int role )
    42. {
    43.  
    44. QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
    45. QString id = data(primaryKeyIndex, role).toString();
    46.  
    47. clear();
    48.  
    49. bool ok;
    50. if (index.column() == 0) {
    51. ok = setSelected(id, value.toInt());
    52. }
    53. refresh();
    54. return ok;
    55. }
    56. //! [1]
    57.  
    58. void selectionSetModel::refresh()
    59. {
    60. setQuery("select selected, selectionset, filecount, size from selectionsets");
    61. setHeaderData(0, Qt::Horizontal, QObject::tr("Selected"),Qt::CheckStateRole);
    62. setHeaderData(1, Qt::Horizontal, QObject::tr("Selection Set"));
    63. setHeaderData(2, Qt::Horizontal, QObject::tr("File Count"));
    64. setHeaderData(2, Qt::Horizontal, QObject::tr("Size"));
    65. }
    66.  
    67. //! [2]
    68. bool selectionSetModel::setSelected(QString id, qint8 selected)
    69. {
    70. QSqlQuery query;
    71. query.prepare("update selectionsets set id = ? where selected = ?");
    72. query.addBindValue(id);
    73. query.addBindValue(selected);
    74. return query.exec();
    75. }
    76. //! [2]
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Checkbox in QListView

    You are returning a bool for Qt::CheckStateRole but the are four possible states. The values should be one of: Qt::Checked, Qt::Unchecked, Qt::Tristate (partially checked), or a null QVariant (for no check box at all)

  5. #5
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Checkbox in QListView

    Sorry for the late response.

    To be honest, I not sure how to do this. Could you update my code and post it back to this thread.

    Regards

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Checkbox in QListView

    Since you only seem to want on/off:
    Qt Code:
    1. return selected.toInt()? Qt::Checked: Qt::Unchecked;
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to ChrisW67 for this useful post:

    silmaril (31st January 2014)

Similar Threads

  1. checkbox on QListView
    By ken123 in forum Qt Programming
    Replies: 2
    Last Post: 18th July 2013, 12:00
  2. Replies: 4
    Last Post: 1st June 2011, 14:54
  3. checkbox
    By fluefiske in forum Newbie
    Replies: 3
    Last Post: 5th October 2010, 22:41
  4. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 21:59
  5. checkBox
    By mickey in forum Qt Programming
    Replies: 8
    Last Post: 1st April 2006, 23:05

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.