Results 1 to 17 of 17

Thread: combobox and item colors

  1. #1
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default combobox and item colors

    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.

    thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: combobox and item colors

    Qt Code:
    1. int index = ...;
    2. comboBox->setItemData(index, Qt::red, Qt::DecorationRole);
    3. // or
    4. comboBox->setItemData(index, Qt::green, Qt::BackgroundRole);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    ¡¡¡¡¡¡ 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?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: combobox and item colors

    Perhaps you could make the highlight color transparent:
    Qt Code:
    1. QPalette pal = comboBox->palette(); // or comboBox->view()
    2. pal.setColor(QPalette::Highlight, Qt::transparent); // or partly transparent
    3. comboBox->setPalette(pal); // or comboBox->view()
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    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.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: combobox and item colors

    Perhaps this is closer to what you want:
    Qt Code:
    1. int size = comboBox->style()->pixelMetric(QStyle::PM_SmallIconSize);
    2. QPixmap pixmap(size, size);
    3. pixmap.fill(color);
    4. comboBox->setItemData(index, pixmap, Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    This works like you post first.

    Here is my code:

    Qt Code:
    1. QStringList colorNames;
    2. colorNames <<"black"<<"white"<<"darkGray"<<"gray"<<"lightGray"<<"red"<<"green"<<"blue"<<"cyan"<<"magenta"<<"yellow"<<"darkRed"<<"darkGreen"<<"darkBlue"<<"darkCyan"<<"darkMagenta"<<"darkYellow";
    3.  
    4. CMBcolor = new QComboBox();
    5. CMBcolor->setMinimumWidth(40);
    6. CMBcolor->setMaximumWidth(40);
    7. QPalette pal = CMBcolor->palette(); // or comboBox->view()
    8. pal.setColor(QPalette::Highlight, Qt::transparent); // or partly transparent
    9. CMBcolor->setPalette(pal); // or comboBox->view()
    10.  
    11. int size = CMBcolor->style()->pixelMetric(QStyle::PM_SmallIconSize);
    12. QPixmap pixmap(size, size);
    13.  
    14. int cont=0;
    15. foreach (QString name, colorNames){
    16. CMBcolor->addItem("", QColor(cont));
    17. //CMBcolor->setItemData(cont, QColor(name), Qt::DecorationRole);
    18. pixmap.fill(QColor(name));
    19. CMBcolor->setItemData(cont, pixmap, Qt::DecorationRole);
    20. cont=cont+1;
    21. }
    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.
    Attached Images Attached Images

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: combobox and item colors

    Which version of Qt are you using? Seems to work for me with Qt 4.2.3 and 4.3.1:
    Attached Images Attached Images
    J-P Nurmi

  9. #9
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    version 4.1.4 comercial for windows
    Last edited by zorro68; 22nd September 2007 at 03:59.

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: combobox and item colors

    So, consider updating..
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    zorro68 (22nd September 2007)

  12. #11
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    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???

  13. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: combobox and item colors

    Quote Originally Posted by zorro68 View Post
    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..
    J-P Nurmi

  14. #13
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    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??
    Last edited by zorro68; 23rd September 2007 at 12:28.

  15. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: combobox and item colors

    So try adding environment variable:
    QTDIR=C:\Qt\4.3.1
    (or wherever your Qt is installed to) and restart visual studio.
    J-P Nurmi

  16. #15
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    I thought that this is the problem, but where must i add this environment variable, in windows, visual studio(how) or qt (how)

  17. #16
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    I have tryed in windows and work fine.
    Now I have another error:

    LINK : fatal error LNK1181: cannot open the file 'QtOpenGLd.lib'

  18. #17
    Join Date
    Jan 2007
    Posts
    95
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: combobox and item colors

    All fine. This lib now is called QtOpenGld4.lib


    Thanks very much jpn

Similar Threads

  1. Retrieve userData of a comboBox item
    By jiveaxe in forum Newbie
    Replies: 6
    Last Post: 23rd August 2007, 10:28
  2. Replies: 8
    Last Post: 15th May 2007, 09:21
  3. Can Qtable Item be a comboBox?
    By iGoo in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2006, 09:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.