{
//If you remove this line and comment everything else, you'll get the same result as the demo examples. If you dont, you get a colored rect, but not updating accordingly
//QItemDelegate::paint(painter, option, index);
QColor color
= index.
data().
Color;
painter->setBrush(Qt::SolidPattern);
painter->setBrush(color);
painter->setPen(thisPen);
painter->drawRect(option.rect.x() + 5, option.rect.y() + 5, 20, 20);
painter->drawText(option.rect.x() + 27, option.rect.y() + 20, color.name());
}
{
QString cadena
= index.
data().
toString();
{
}
int indice = combo->findText(cadena);
if (indice >= 0)
{
combo->setCurrentIndex(indice);
}
// colorName. cadena;
return combo;
}
{
QString cadena
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
QComboBox *comboBox
= qobject_cast<QComboBox
*>
(editor
);
int indice = comboBox->findText(cadena);
if (indice >= 0)
comboBox->setItemText(indice, cadena);
}
void ColorlistDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//If you remove this line and comment everything else, you'll get the same result as the demo examples. If you dont, you get a colored rect, but not updating accordingly
//QItemDelegate::paint(painter, option, index);
QPen thisPen;
QColor color = index.data().Color;
painter->setBrush(Qt::SolidPattern);
painter->setBrush(color);
painter->setPen(thisPen);
painter->drawRect(option.rect.x() + 5, option.rect.y() + 5, 20, 20);
painter->drawText(option.rect.x() + 27, option.rect.y() + 20, color.name());
}
QWidget *ColorlistDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *combo = new QComboBox(parent);
QString cadena = index.data().toString();
QStringList colorNames = QColor::colorNames();
foreach (QString name, colorNames)
{
QPixmap pix(24, 24);
pix.fill(QColor(name));
combo->addItem(QIcon(pix), name, QColor(name));
}
int indice = combo->findText(cadena);
if (indice >= 0)
{
combo->setCurrentIndex(indice);
}
// colorName. cadena;
return combo;
}
void ColorlistDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString cadena = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
int indice = comboBox->findText(cadena);
if (indice >= 0)
comboBox->setItemText(indice, cadena);
}
To copy to clipboard, switch view to plain text mode
Bookmarks