PDA

View Full Version : color comboBox problem



chguy2
20th April 2007, 14:48
Hi guys,

I have a comboBox that displays colors with the last item that displays “other”.

My code looks like:

comboBox_colors->addItem(“”);
comboBox_colors->setItemData(0,Qt::red,Qt::BackgroundColorRole);
comboBox_colors->addItem(“”);
comboBox_colors->setItemData(1,Qt::blue,Qt::BackgroundColorRole);
comboBox_colors->addItem(“”);
comboBox_colors->setItemData(2,Qt::green,Qt::BackgroundColorRole);
…
comboBox_colors->addItem(“other”);
comboBox_colors->setItemData(15,Qt::transparent,Qt::BackgroundColor Role);

I am having trouble disabling the highlight option because now when I scroll over the colors they change from whatever the color is to the highlight color. I tried changing the highlight color to white but it just changes the color from whatever to white.
I am also having trouble displaying the selected color in the comboBox when it is minimized. No matter what color is selected it will not display when the comboBox is minimized. So it always has a white background when the comboBox is minimized. The only time when I see something in the comboBox is when “other” is selected because “other” is displayed. Obviously if I put a word in the “” when I add the item, it will get displayed but I want it to display the color and no words.

If someone could help me out with this, it would be greatly appreciated. I have been suck on this for awhile.


*Maybe instead of disabling the highlight I can just set it to transparent? How do you change the color of the highlight in a comboBox in code? I only know how to change it using QT designer.*

I tried:

QPalette highlight;
QBrush trans(Qt::transparent);
trans.setStyle(Qt::SolidPattern);
highlight.setBrush(QPalette::Highlight,trans);
comboBox_colors->setPalette(highlight);
But that makes it white and not transparent.

Thanks.

jpn
21st April 2007, 08:47
I'm not convinced changing the palette like that is a good idea. How would you see the selection at all? :)

Anyway, after taking a look at qitemdelegate.cpp I noticed that also a color is supported as decoration role (I'm not sure since when, but presumably at least in 4.2). So here's yet another proposal in addition to those in thread Color combobox (http://www.qtcentre.org/forum/f-qt-programming-2/t-color-combobox-6417.html):


comboBox->addItem("red");
comboBox->setItemData(0, QColor(Qt::red), Qt::DecorationRole);

wysota
22nd April 2007, 09:57
color is supported as decoration role (I'm not sure since when, but presumably at least in 4.2).

AFAIR Since 4.0