Results 1 to 6 of 6

Thread: Checkbox in QListView

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.