【转】Qt中QTableView中加入Check列实现

class MyModel : public QSqlQueryModel {
Q_OBJECT
public:
MyModel(QObject *parent = 0);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
........
};
 
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const {
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == aColWithCheckbox)
flags |= Qt::ItemIsUserCheckable;
else
flags |= Qt::ItemIsEditable;
return flags;
}
 
QVariant MyModel::data(const QModelIndex &index,  int role) const {
QVariant value = QSqlQueryModel::data(index, role);
if (role == Qt::CheckStateRole && index.column() == aColWithCheckbox)
return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
else
return value;
}
posted @ 2010-07-13 12:51 阅读(610) posted @ 2010-07-13 12:51 阅读(610)
class MyModel : public QSqlQueryModel { Q_OBJECT public: MyModel(QObject *parent = 0); Qt::ItemFlags flags(const QModelIndex &index) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; ........ };   Qt::ItemFlags MyModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QSqlQueryModel::flags(index); if (index.column() == aColWithCheckbox) flags |= Qt::ItemIsUserCheckable; else flags |= Qt::ItemIsEditable; return flags; }   QVariant MyModel::data(const QModelIndex &index, int role) const { QVariant value = QSqlQueryModel::data(index, role); if (role == Qt::CheckStateRole && index.column() == aColWithCheckbox) return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked; else return value; } posted @ 2010-07-13 12:51 阅读(610)
经验分享 程序员 微信小程序 职场和发展