if (index.column() == 4) {
}
}
QColor baseColor
= QColor(index.
sibling(index.
row(),
5).
data().
toInt(),
index.sibling(index.row(), 6).data().toInt(),
index.sibling(index.row(), 7).data().toInt(),
index.sibling(index.row(), 8).data().toInt());
QColorDialog *pDialog
= qobject_cast<QColorDialog
*>
(editor
);
if(pDialog)
pDialog->setCurrentColor(baseColor);
}
QColorDialog *pDialog
= qobject_cast<QColorDialog
*>
(editor
);
if(pDialog) {
int resultCode = pDialog->result();
qDebug() << "Result Code : " << resultCode;
// Here i only get QDialog::Rejected, no matter if the "OK" or the "Cancel" button was pressed
if(resultCode
== QDialog::Accepted) { QColor currentColor
= pDialog
->currentColor
();
model->setData(index.model()->index(index.row(), 5), currentColor.red(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 6), currentColor.green(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 7), currentColor.blue(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 8), currentColor.alpha(), Qt::EditRole);
}
}
}
if(option.
state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight());
QColor baseColor
(index.
sibling(index.
row(),
5).
data().
toInt(),
index.sibling(index.row(), 6).data().toInt(),
index.sibling(index.row(), 7).data().toInt(),
index.sibling(index.row(), 8).data().toInt());
painter->save();
painter->setBrush(baseColor);
painter->setPen(Qt::black);
painter->drawRect(option.rect.x()+2, option.rect.y()+2,option.rect.width()-5, option.rect.height()-5);
painter->restore();
}
QWidget* MyDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
if (index.column() == 4) {
return new QColorDialog(parent);
}
return QItemDelegate::createEditor(parent, option, index);
}
void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
QColor baseColor = QColor(index.sibling(index.row(), 5).data().toInt(),
index.sibling(index.row(), 6).data().toInt(),
index.sibling(index.row(), 7).data().toInt(),
index.sibling(index.row(), 8).data().toInt());
QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
if(pDialog)
pDialog->setCurrentColor(baseColor);
}
void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const {
QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
if(pDialog) {
int resultCode = pDialog->result();
qDebug() << "Result Code : " << resultCode;
// Here i only get QDialog::Rejected, no matter if the "OK" or the "Cancel" button was pressed
if(resultCode == QDialog::Accepted) {
QColor currentColor = pDialog->currentColor();
model->setData(index.model()->index(index.row(), 5), currentColor.red(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 6), currentColor.green(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 7), currentColor.blue(), Qt::EditRole);
model->setData(index.model()->index(index.row(), 8), currentColor.alpha(), Qt::EditRole);
}
}
}
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
if(option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
QColor baseColor(index.sibling(index.row(), 5).data().toInt(),
index.sibling(index.row(), 6).data().toInt(),
index.sibling(index.row(), 7).data().toInt(),
index.sibling(index.row(), 8).data().toInt());
painter->save();
painter->setBrush(baseColor);
painter->setPen(Qt::black);
painter->drawRect(option.rect.x()+2, option.rect.y()+2,option.rect.width()-5, option.rect.height()-5);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks