QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;void setEditorData(QWidget *editor, const QModelIndex &index) const;void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QWidget *GuiDelegateTree::createEditor(QWidget *parent, const QStyleOptionViewItem & option , const QModelIndex &index ) const{ // некая проверка .... QCheckBox *editor = new QCheckBox(parent); editor->installEventFilter(const_cast<GuiDelegateTree*>(this)); return editor;}void GuiDelegateTree::setEditorData(QWidget *editor, const QModelIndex &index) const{ Qt::CheckState value = (Qt::CheckState)(index.model()->data(index, Qt::CheckStateRole).toInt()); QCheckBox *checkBox = static_cast<QCheckBox*>(editor); checkBox->setCheckState(value);}void GuiDelegateTree::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QCheckBox *checkBox = static_cast<QCheckBox*>(editor); int value = (int)(checkBox->checkState()); model->setData(index, value, Qt::CheckStateRole); }void GuiDelegateTree::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const{ QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 13, 13); editor->setGeometry(r); }
#include <QtGui>#include <QtSql>class TestModel: public QAbstractTableModel{public: QVariant m_values[3][3]; QVariant m_checks[3][3]; TestModel(QObject * parent = 0): QAbstractTableModel(parent) { int row, column; for(row=0;row<3;row++) for(column=0;column<3;column++) { m_values[row][column] = QVariant( QString("%L1:%L2").arg(row).arg(column) ); m_checks[row][column] = QVariant( (row+column%2) ? Qt::Checked : Qt::Unchecked ); } } int rowCount( const QModelIndex & parent = QModelIndex() ) const { return 3; } int columnCount( const QModelIndex & parent = QModelIndex() ) const { return 3; } virtual Qt::ItemFlags flags( const QModelIndex & index ) const { int column = index.column(); switch( column ) { case 0: return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable; case 1: return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsUserCheckable; case 2: return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable; default:return 0; } } virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole) const { int row = index.row(); int column = index.column(); switch( role ) { case Qt::DisplayRole: case Qt::EditRole: return m_values[row][column]; case Qt::CheckStateRole: if ( column>0 ) return m_checks[row][column]; else return QVariant(); default: return QVariant(); } } virtual bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) { int row = index.row(); int column = index.column(); switch( role ) { case Qt::EditRole: m_values[row][column] = value; return true; case Qt::CheckStateRole: m_checks[row][column] = value; return true; default: return false; } }};QTableView *createView(const QString &title, QAbstractTableModel *model){ QTableView *view = new QTableView; view->setModel(model); view->setWindowTitle(title); return view;}int main(int argc, char *argv[]){ QApplication app(argc, argv); TestModel model; QTableView *view = createView(QObject::tr("My Table"), &model); view->show(); return app.exec();}