Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Created January 17, 2017 21:04
Show Gist options
  • Save peteristhegreat/f70ff6098387f2884a190775203dd981 to your computer and use it in GitHub Desktop.
Save peteristhegreat/f70ff6098387f2884a190775203dd981 to your computer and use it in GitHub Desktop.
Checkable/Checkbox SQL item in Qt QTableView
#include "checkablesortfilterproxymodel.h"
CheckableSortFilterProxyModel::CheckableSortFilterProxyModel(QObject *parent) :
QSortFilterProxyModel(parent)
{
}
void CheckableSortFilterProxyModel::setParameters(QList<int> boolCols) {
booleanSet.clear();
if (!boolCols.isEmpty()) {
foreach(int column , boolCols)
{
booleanSet.append(column);
}
}
}
QVariant CheckableSortFilterProxyModel::data(const QModelIndex &index, int role) const {
if(!index.isValid())
return QVariant();
if(booleanSet.contains(index.column()) && (role == Qt::CheckStateRole || role == Qt::DisplayRole)) {
if (role == Qt::CheckStateRole)
return index.data(Qt::EditRole).toBool() ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
else if (role == Qt::DisplayRole)
return QVariant();
}
else
return QSortFilterProxyModel::data(index,role);
}
bool CheckableSortFilterProxyModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if(!index.isValid())
return false;
if(booleanSet.contains(index.column()) && role==Qt::CheckStateRole)
{
QVariant data = (value.toInt()==Qt::Checked) ? QVariant(1) : QVariant (0);
return QSortFilterProxyModel::setData(index, data, Qt::EditRole);
}
else
return QSortFilterProxyModel::setData(index,value,role);
}
Qt::ItemFlags CheckableSortFilterProxyModel::flags ( const QModelIndex & index ) const {
if(!index.isValid())
return Qt::ItemIsEnabled;
if(booleanSet.contains(index.column()))
return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
else
return QSortFilterProxyModel::flags(index);
}
// based off of https://forum.qt.io/topic/4716/qtableview-checkbox-delegate-alignmnent/43
// which is based off of http://www.qtcentre.org/archive/index.php/t-18675.html
#ifndef CHECKABLESORTFILTERPROXYMODEL_H
#define CHECKABLESORTFILTERPROXYMODEL_H
#include <QSortFilterProxyModel>
class CheckableSortFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit CheckableSortFilterProxyModel(QObject *parent = 0);
void setParameters(QList<int> boolCols);
protected:
QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
Qt::ItemFlags flags ( const QModelIndex & index ) const;
signals:
public slots:
private:
QList<int> booleanSet;
};
#endif // CHECKABLESORTFILTERPROXYMODEL_H
// example usage
#include "checkablesortfilterproxymodel.h"
// ...
CheckableSortFilterProxyModel *cfpm = new CheckableSortFilterProxyModel(this);
QList<int> boolCols;
boolCols.append( usrModel->fieldIndex("isActive") );
boolCols.append( usrModel->fieldIndex("isOk") );
cfpm->setParameters(boolCols);
cfpm->setSourceModel( mySqlTableModel );
myTableView->setModel(cfpm);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment