1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
class CheckComboBoxDelegate : public QStyledItemDelegate
{
public:
CheckComboBoxDelegate(QObject *parent = 0, Parametres* pref=0) : QStyledItemDelegate(parent) {
this->parametres = parametres;
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
bool value = index.data(Qt::UserRole).toBool();
const QString text = index.data(Qt::DisplayRole).toString();
const QStyleOptionViewItemV4 sov = static_cast<const QStyleOptionViewItemV4>(option);
const QStyle *style = sov.widget->style();
QStyleOptionButton opt;
if(text == case_x )
value = (/*Le paramètre est à true*/) ? true: false;
opt.state |= value ? QStyle::State_On : QStyle::State_Off;
opt.state |= QStyle::State_Enabled;
opt.text = text;
opt.rect = option.rect;
style->drawControl(QStyle::CE_CheckBox, &opt,painter);
}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
Q_UNUSED(option);
Q_UNUSED(index);
QCheckBox *checkBox = new QCheckBox(parent);
return checkBox;
}
// fonction appellée au survol d'une case
void setEditorData(QWidget *editor, const QModelIndex &index) const {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor);
if (checkBox) {
QString text = index.data(Qt::DisplayRole).toString();
bool value = index.data(Qt::UserRole).toBool();
if(text == case_x )
value = (/*Le paramètre est à true*/) ? true: false;
checkBox->setText(text);
checkBox->setChecked(value);
}
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor);
if (checkBox) {
model->setData(index, checkBox->isChecked(), Qt::UserRole);
QString text = index.data(Qt::DisplayRole).toString();
if(text == case_x)
parametre = (checkBox->isChecked()) ? "1" : "0";
}
/*
QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor);
if (checkBox) {
model->setData(index, checkBox->isChecked(), Qt::UserRole);
qDebug() << Q_FUNC_INFO;
}*/
//if(checkBox->isChecked())
// std::cout << "case checked : " << index.data(0).toString().toStdString() << std::endl;
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
private :
Parametres* parametres;
};
#endif |
Partager