MyChBDelegate::MyChBDelegate(QObject *parent) : QItemDelegate(parent){}QWidget *MyChBDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem& /* option */,const QModelIndex& index ) const{ QCheckBox *editor = new QCheckBox(parent); int value = index.model()->data(index,Qt::EditRole).toInt(); editor->setCheckState(value != 0 ? Qt::Checked : Qt::Unchecked); editor->installEventFilter(const_cast<MyChBDelegate*>(this)); return editor;}void MyChBDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const{ int value = index.model()->data(index, Qt::EditRole).toInt(); QCheckBox *dsb = static_cast<QCheckBox*>(editor); dsb->setCheckState(value != 0 ? Qt::Checked : Qt::Unchecked);}void MyChBDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex& index) const{ QCheckBox *dsb = static_cast<QCheckBox*>(editor); bool value = dsb->checkState(); model->setData(index, value);}void MyChBDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option,const QModelIndex& /* index */) const{ editor->setGeometry(option.rect);}void MyChBDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QVariant value = index.model()->data(index,Qt::EditRole); QString text = value.toInt() != 0 ? QString::fromUtf8("Да") : QString::fromUtf8("Нет"); QPalette palet; palet.setColor(QPalette::Text,Qt::red); palet.setColor(QPalette::HighlightedText,Qt::green); palet.setColor(QPalette::Highlight,Qt::yellow); QStyleOptionViewItem myOption = option; myOption.palette=palet; myOption.displayAlignment = Qt::AlignCenter; drawDisplay(painter,myOption,myOption.rect,text); drawFocus(painter,myOption,myOption.rect);}
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QSqlTableModel::flags(index); if (index.column() > 1 && index.column() < 3) flags |= Qt::ItemIsEditable; if (index.column() == 3) flags |= Qt::ItemIsUserCheckable; return flags; } QVariant MyModel::data(const QModelIndex &index,int role) const { QVariant value = QSqlTableModel::data(index, role); switch (role) { . . . case Qt::CheckStateRole: if (index.column() == 3) return (QSqlTableModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked; else return value; . . . } return value; }
C++ (Qt) drawCheckBox(QStyle::State_Off); drawCheckBox(QStyle::State_On); drawCheckBox(QStyle::State_Enabled | QStyle::State_Off); drawCheckBox(QStyle::State_Enabled | QStyle::State_On); drawCheckBox(QStyle::State_Enabled | QStyle::State_Off | QStyle::State_MouseOver); drawCheckBox(QStyle::State_Enabled | QStyle::State_On | QStyle::State_MouseOver); drawCheckBox(QStyle::State_Enabled | QStyle::State_Off | QStyle::State_MouseOver | QStyle::State_Sunken); drawCheckBox(QStyle::State_Enabled | QStyle::State_On | QStyle::State_MouseOver | QStyle::State_Sunken); QPixmap drawCheckBox(QStyle::State State, QWidget* w) { QStyle* Style = QApplication::style(); QStyleOptionButton StyleOptionButton; if(w) {StyleOptionButton.initFrom(w);} StyleOptionButton.state = State; QRect r = Style->subElementRect (QStyle::SE_CheckBoxIndicator, &StyleOptionButton); StyleOptionButton.rect = r; if(!(State&QStyle::State_Enabled)) { QPalette p = QApplication::palette(); p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Disabled, QPalette::Text)); StyleOptionButton.palette = p; } QPixmap pixmap(r.width(), r.height()); pixmap.fill(Qt::transparent); QPainter painter; painter.begin(&pixmap); painter.translate(-r.left(), -r.top()); Style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &StyleOptionButton, &painter); painter.end(); return pixmap;}
Qt::ItemFlags QPGTableModel::flags(const QModelIndex &index) const{ Qt::ItemFlags flags = QSqlTableModel::flags(index); if (index.column() == 4) flags |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable; return flags;}QVariant QPGTableModel::data(const QModelIndex &index,int role) const{ QVariant value = QSqlTableModel::data(index, role); switch (role) { case Qt::DisplayRole: case Qt::EditRole: if(index.column()==4) return value.toInt() != 0 ? QString::fromUtf8("Да") : QString::fromUtf8("Нет"); return QSqlTableModel::data(index,role); case Qt::CheckStateRole: if (index.column() == 4) return (QSqlTableModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked; } return value;}bool QPGTableModel::setData(const QModelIndex &index,const QVariant &value,int role){ if(role==Qt::CheckStateRole) return QSqlTableModel::setData(index,value.toBool(),Qt::EditRole); return QSqlTableModel::setData(index,value);}
C++ (Qt)#ifndef __CHECKBOX_DELEGATE_H__#define __CHECKBOX_DELEGATE_H__ #include <QItemDelegate> class CChecBox_Delegate : public QItemDelegate{ Q_OBJECT; QString m_sIconPath; QString m_sText; public: CChecBox_Delegate( QObject * parent = 0 ); 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; void paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const; inline void setIconPath( QString sVal ) { m_sIconPath = sVal; } inline void setText( QString sVal ) { m_sText = sVal; }}; #endif // __CHECKBOX_DELEGATE_H__
C++ (Qt) #include "CheckBox_Delegate.h" #include <QCheckBox>#include <QStylePainter>#include <QPalette>#include <QAbstractItemView>#include <QDebug> CChecBox_Delegate::CChecBox_Delegate( QObject *parent ) : QItemDelegate(parent){ m_sIconPath = ""; m_sText = "";} QWidget * CChecBox_Delegate::createEditor( QWidget * parent, const QStyleOptionViewItem& /* option */, const QModelIndex& index ) const{ QCheckBox * pEditor = new QCheckBox( parent ); pEditor->setText( m_sText ); pEditor->setCheckable( true ); pEditor->setIcon( QIcon( m_sIconPath ) ); pEditor -> installEventFilter( const_cast<CChecBox_Delegate*>( this ) ); return pEditor;} void CChecBox_Delegate::setEditorData( QWidget * editor, const QModelIndex &index) const{ int value = index.model()->data( index, Qt::EditRole ).toInt(); QCheckBox * cb = static_cast<QCheckBox*>( editor ); cb->setChecked( value );} void CChecBox_Delegate::setModelData( QWidget * editor, QAbstractItemModel *model, const QModelIndex& index) const{ QCheckBox * cb = static_cast<QCheckBox*>( editor ); model->setData( index, cb->isChecked() );} void CChecBox_Delegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index ) const{ editor->setGeometry( option.rect );} void CChecBox_Delegate::paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const{ QWidget * w = dynamic_cast<QWidget *>( painter->device() ); if ( w ) { QStylePainter p( w ); QStyleOptionButton opt; opt.text = m_sText; opt.icon = QIcon( m_sIconPath ); opt.iconSize = QSize( 24, 24 ); opt.initFrom( w ); opt.rect = option.rect; bool bChecked = index.data().toInt() > 0; if ( bChecked ) opt.state |= QStyle::State_On; p.drawControl( QStyle::CE_CheckBox, opt ); } drawFocus( painter, option, option.rect );}
C++ (Qt)QItemEditorFactory *factory = new QItemEditorFactory;factory->registerEditor(QVariant::Bool, new QStandardItemEditorCreator<QCheckBox>());QItemEditorFactory::setDefaultFactory(factory);