This works like you post first.
Here is my code:
colorNames <<"black"<<"white"<<"darkGray"<<"gray"<<"lightGray"<<"red"<<"green"<<"blue"<<"cyan"<<"magenta"<<"yellow"<<"darkRed"<<"darkGreen"<<"darkBlue"<<"darkCyan"<<"darkMagenta"<<"darkYellow";
CMBcolor->setMinimumWidth(40);
CMBcolor->setMaximumWidth(40);
QPalette pal
= CMBcolor
->palette
();
// or comboBox->view() pal.
setColor(QPalette::Highlight, Qt
::transparent);
// or partly transparentCMBcolor->setPalette(pal); // or comboBox->view()
int size
= CMBcolor
->style
()->pixelMetric
(QStyle::PM_SmallIconSize);
int cont=0;
foreach
(QString name, colorNames
){ CMBcolor
->addItem
("",
QColor(cont
));
//CMBcolor->setItemData(cont, QColor(name), Qt::DecorationRole);
CMBcolor->setItemData(cont, pixmap, Qt::DecorationRole);
cont=cont+1;
}
QStringList colorNames;
colorNames <<"black"<<"white"<<"darkGray"<<"gray"<<"lightGray"<<"red"<<"green"<<"blue"<<"cyan"<<"magenta"<<"yellow"<<"darkRed"<<"darkGreen"<<"darkBlue"<<"darkCyan"<<"darkMagenta"<<"darkYellow";
CMBcolor = new QComboBox();
CMBcolor->setMinimumWidth(40);
CMBcolor->setMaximumWidth(40);
QPalette pal = CMBcolor->palette(); // or comboBox->view()
pal.setColor(QPalette::Highlight, Qt::transparent); // or partly transparent
CMBcolor->setPalette(pal); // or comboBox->view()
int size = CMBcolor->style()->pixelMetric(QStyle::PM_SmallIconSize);
QPixmap pixmap(size, size);
int cont=0;
foreach (QString name, colorNames){
CMBcolor->addItem("", QColor(cont));
//CMBcolor->setItemData(cont, QColor(name), Qt::DecorationRole);
pixmap.fill(QColor(name));
CMBcolor->setItemData(cont, pixmap, Qt::DecorationRole);
cont=cont+1;
}
To copy to clipboard, switch view to plain text mode
But when i click in one color, the selected item in the combo is always white. And the selected item background color should be the color i have selected.
Show the picture.
Bookmarks