View Full Version : combobox and item colors
zorro68
20th September 2007, 03:10
My Qt version is 4.1.
Im trying to change the background color of each item (one diferent color for each item) in a combobox. How can I select an item in a combobox? How can i change the color of an item?
I have read a lot in post and i know how to programm a combo with the same color for all items (Qpalette, setcolor, setpalette,...) but i want to change the background color of each item. :confused:
thanks
jpn
20th September 2007, 07:34
int index = ...;
comboBox->setItemData(index, Qt::red, Qt::DecorationRole);
// or
comboBox->setItemData(index, Qt::green, Qt::BackgroundRole);
zorro68
20th September 2007, 20:22
¡¡¡¡¡¡ So easy !!!!!. I have just programm this and works fine
Thank very much jpn.
Another question: how can I say that not highlight when i select an item?
jpn
20th September 2007, 21:09
Perhaps you could make the highlight color transparent:
QPalette pal = comboBox->palette(); // or comboBox->view()
pal.setColor(QPalette::Highlight, Qt::transparent); // or partly transparent
comboBox->setPalette(pal); // or comboBox->view()
zorro68
21st September 2007, 20:09
I have prove this but dont works like i want.
As you know, in a combobox, when you select an item, the list of the combo close and you can see the highlight selected item only. But my combo is for choose colors, and when i select one item the highlight don't permits that i can see the real color.
With your solution, when i pick a combo item, the background of the selected item is always white (transparent). What to do?
Excuse me for my bad english. I hope you understand my problem.
jpn
21st September 2007, 21:22
Perhaps this is closer to what you want:
int size = comboBox->style()->pixelMetric(QStyle::PM_SmallIconSize);
QPixmap pixmap(size, size);
pixmap.fill(color);
comboBox->setItemData(index, pixmap, Qt::DecorationRole);
zorro68
21st September 2007, 23:56
This works like you post first.
Here is my code:
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;
}
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.
jpn
22nd September 2007, 00:24
Which version of Qt are you using? Seems to work for me with Qt 4.2.3 and 4.3.1:
zorro68
22nd September 2007, 03:41
version 4.1.4 comercial for windows
jpn
22nd September 2007, 06:03
So, consider updating.. :)
zorro68
23rd September 2007, 02:59
I have updated to 4.3.1 but now i have an error when compiling the programm:
Project : error PRJ0019: Una herramienta devolvió un código de error de "MOC visor.h"
1>Proyecto : warning PRJ0018 : No se encontraron las siguientes variables de entorno:
1>$(QTDIR)
I have compiling the qt4.3.1 and all ok.
I have change my visual studio 2005 proyect, where was written 4.1.4 I have change to 4.3.1 but dont work. I have create a new proyect but occurr the same.
I dont know why???? Can you help me???
jpn
23rd September 2007, 10:52
Project : error PRJ0019: Una herramienta devolvió un código de error de "MOC visor.h"
1>Proyecto : warning PRJ0018 : No se encontraron las siguientes variables de entorno:
Could you translate these to english, please? I guess there's something wrong with visor.h but hard to say what without seeing the file..
zorro68
23rd September 2007, 12:17
This is while i compile the whole project:
Project : error PRJ0019: A tools returned an error code from"MOC visor.h"
1>Project : warning PRJ0018 : The following environment variables were not found
1>$(QTDIR)
If i compile one file by one, i have another error in other file:
1>.\glwidget.cpp(2) : fatal error C1083: cannot open Include file: 'QtOpenGL': No such file or directory
Has changed somethig with Opengl??
jpn
23rd September 2007, 12:24
So try adding environment variable:
QTDIR=C:\Qt\4.3.1
(or wherever your Qt is installed to) and restart visual studio.
zorro68
23rd September 2007, 12:32
I thought that this is the problem, but where must i add this environment variable, in windows, visual studio(how) or qt (how)
zorro68
23rd September 2007, 12:37
I have tryed in windows and work fine.
Now I have another error:
LINK : fatal error LNK1181: cannot open the file 'QtOpenGLd.lib'
zorro68
23rd September 2007, 12:54
All fine. This lib now is called QtOpenGld4.lib
Thanks very much jpn
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.