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:
selectionSetModel *selectionViewModel = new selectionSetModel();
selectionViewModel->setQuery("select selected, selectionset, filecount, size from selectionsets");
selectionViewModel
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("Selected"));
selectionViewModel
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Selection Set"));
selectionViewModel
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("File Count"));
selectionViewModel
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("Size"));
ui->selectionsetTableView->setModel(selectionViewModel);
selectionSetModel *selectionViewModel = new selectionSetModel();
selectionViewModel->setQuery("select selected, selectionset, filecount, size from selectionsets");
selectionViewModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Selected"));
selectionViewModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Selection Set"));
selectionViewModel->setHeaderData(2, Qt::Horizontal, QObject::tr("File Count"));
selectionViewModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Size"));
ui->selectionsetTableView->setModel(selectionViewModel);
To copy to clipboard, switch view to plain text mode
#include <QtSql>
#include <QTextCharFormat>
#include <QtDebug>
#include "includes/selectionsetmodel.h"
{
}
Qt
::ItemFlags selectionSetModel
::flags(const QModelIndex &index
) const{
if(index.column()==0)
{
Qt::ItemFlags flags;
flags |= Qt::ItemIsSelectable;
flags |= Qt::ItemIsEditable;
flags |= Qt::ItemIsEnabled;
flags |= Qt::ItemIsUserCheckable;
return flags;
}
else
{
}
}
{
if (index.column() == 0)
{
if ( index.column() == 0 )
{
// Change this to suit your particular model storage backend
qDebug() << "Returned checked";
return Qt::Checked;
}
}
else
{
return value;
}
}
{
QString id
= data
(primaryKeyIndex, role
).
toString();
clear();
bool ok;
if (index.column() == 0) {
ok = setSelected(id, value.toInt());
}
refresh();
return ok;
}
//! [1]
void selectionSetModel::refresh()
{
setQuery("select selected, selectionset, filecount, size from selectionsets");
setHeaderData
(0, Qt
::Horizontal,
QObject::tr("Selected"),Qt
::CheckStateRole);
setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Selection Set"));
setHeaderData
(2, Qt
::Horizontal,
QObject::tr("File Count"));
setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Size"));
}
//! [2]
bool selectionSetModel
::setSelected(QString id, qint8 selected
) {
query.prepare("update selectionsets set id = ? where selected = ?");
query.addBindValue(id);
query.addBindValue(selected);
return query.exec();
}
//! [2]
#include <QtSql>
#include <QTextCharFormat>
#include <QtDebug>
#include "includes/selectionsetmodel.h"
selectionSetModel::selectionSetModel(QObject *parent) : QSqlQueryModel(parent)
{
}
Qt::ItemFlags selectionSetModel::flags(const QModelIndex &index) const
{
if(index.column()==0)
{
Qt::ItemFlags flags;
flags |= Qt::ItemIsSelectable;
flags |= Qt::ItemIsEditable;
flags |= Qt::ItemIsEnabled;
flags |= Qt::ItemIsUserCheckable;
return flags;
}
else
{
return QAbstractItemModel::flags(index);
}
}
QVariant selectionSetModel::data(const QModelIndex &index, int role)
{
QVariant value = QSqlQueryModel::data(index, role);
if (index.column() == 0)
{
if ( index.column() == 0 )
{
// Change this to suit your particular model storage backend
qDebug() << "Returned checked";
return Qt::Checked;
}
}
else
{
return value;
}
}
bool selectionSetModel::setData(const QModelIndex &index, const QVariant &value, int role )
{
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
QString id = data(primaryKeyIndex, role).toString();
clear();
bool ok;
if (index.column() == 0) {
ok = setSelected(id, value.toInt());
}
refresh();
return ok;
}
//! [1]
void selectionSetModel::refresh()
{
setQuery("select selected, selectionset, filecount, size from selectionsets");
setHeaderData(0, Qt::Horizontal, QObject::tr("Selected"),Qt::CheckStateRole);
setHeaderData(1, Qt::Horizontal, QObject::tr("Selection Set"));
setHeaderData(2, Qt::Horizontal, QObject::tr("File Count"));
setHeaderData(2, Qt::Horizontal, QObject::tr("Size"));
}
//! [2]
bool selectionSetModel::setSelected(QString id, qint8 selected)
{
QSqlQuery query;
query.prepare("update selectionsets set id = ? where selected = ?");
query.addBindValue(id);
query.addBindValue(selected);
return query.exec();
}
//! [2]
To copy to clipboard, switch view to plain text mode
#ifndef SELECTIONSETMODEL_H
#define SELECTIONSETMODEL_H
#include <QSqlQueryModel>
{
Q_OBJECT
public:
selectionSetModel
(QObject *parent
= 0);
private:
bool setSelected
(QString id, qint8 selected
);
void refresh();
};
#endif // SELECTIONSETMODEL_H
#ifndef SELECTIONSETMODEL_H
#define SELECTIONSETMODEL_H
#include <QSqlQueryModel>
class selectionSetModel : public QSqlQueryModel
{
Q_OBJECT
public:
selectionSetModel(QObject *parent = 0);
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData (const QModelIndex &index, const QVariant &value, int role);
QVariant data(const QModelIndex &index, int role);
private:
bool setSelected(QString id, qint8 selected);
void refresh();
};
#endif // SELECTIONSETMODEL_H
To copy to clipboard, switch view to plain text mode
Bookmarks