Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, April 2007
setting text color in a QComboBox


Message 1 in thread

Hi,
Sorry if this is a lame question; I've dug through the help and can't 
seem to figure it out.

I want to change the color of the text listed in a QComboBox.
Is this possible?

Mark

-- 
 [ signature omitted ] 

Message 2 in thread

Assuming Qt4.

Are you trying to set the text in the button portion or the menu?  If you're trying to set it in the button portion of the combo box, you need to:

QPalette p = combo->palette()
p.setColor(QPalette::ButtonText, <your color>);
combo->setPalette(p);

If you're trying to change the text in the menus, that is a bit problematic because there is a bug in using the palette to change the text or background in the menu (i.e., QComboBox::view()) of the combo box.  You can use a style sheet instead:

    QString styleSheet = "QComboBox   {  color:   <yourcolor>;   }";
    combo->setStyleSheet(styleSheet);

HTH,
Susan

P.S.  If you need more detailed help on this, I can provide more detailed examples.  The app that I am working on has many custom colors so I've had to set combo box colors not only for active states, but disabled, seletion (highlight), etc.

----- Original Message ----
From: Mark Thompson <mark@xxxxxxxxxxxx>
To: qt-interest@xxxxxxxxxxxxx
Sent: Tuesday, April 24, 2007 11:51:50 AM
Subject: setting text color in a QComboBox

Hi,
Sorry if this is a lame question; I've dug through the help and can't 
seem to figure it out.

I want to change the color of the text listed in a QComboBox.
Is this possible?

Mark

-- 
 [ signature omitted ] 

Message 3 in thread

Susan,
Thanks, that helps a lot.
I would certainly like to see a couple of examples.
Mark

susan@xxxxxxxxxxxx wrote:
> Assuming Qt4.
>
> Are you trying to set the text in the button portion or the menu?  If you're trying to set it in the button portion of the combo box, you need to:
>
> QPalette p = combo->palette()
> p.setColor(QPalette::ButtonText, <your color>);
> combo->setPalette(p);
>
> If you're trying to change the text in the menus, that is a bit problematic because there is a bug in using the palette to change the text or background in the menu (i.e., QComboBox::view()) of the combo box.  You can use a style sheet instead:
>
>     QString styleSheet = "QComboBox   {  color:   <yourcolor>;   }";
>     combo->setStyleSheet(styleSheet);
>
> HTH,
> Susan
>
> P.S.  If you need more detailed help on this, I can provide more detailed examples.  The app that I am working on has many custom colors so I've had to set combo box colors not only for active states, but disabled, seletion (highlight), etc.
>
> ----- Original Message ----
> From: Mark Thompson <mark@xxxxxxxxxxxx>
> To: qt-interest@xxxxxxxxxxxxx
> Sent: Tuesday, April 24, 2007 11:51:50 AM
> Subject: setting text color in a QComboBox
>
> Hi,
> Sorry if this is a lame question; I've dug through the help and can't 
> seem to figure it out.
>
> I want to change the color of the text listed in a QComboBox.
> Is this possible?
>
> Mark
>
>   

-- 
 [ signature omitted ] 

Message 4 in thread

I have attached a compilable test program that shows all the ways to set the
colors in combo boxes (that I know of).  It also shows the bug trying to use
only QPalette to set the foreground/background colors in the menu of the combo
(the trolls are aware of this bug and are actively working it).

--Susan



----- Original Message ----
From: Mark Thompson <mark@xxxxxxxxxxxx>
To: susan@xxxxxxxxxxxx
Cc: qt-interest@xxxxxxxxxxxxx
Sent: Tuesday, April 24, 2007 12:40:36 PM
Subject: Re: setting text color in a QComboBox

Susan,
Thanks, that helps a lot.
I would certainly like to see a couple of examples.
Mark

susan@xxxxxxxxxxxx wrote:
> Assuming Qt4.
>
> Are you trying to set the text in the button portion or the menu?  If you're
trying to set it in the button portion of the combo box, you need to:
>
> QPalette p = combo->palette()
> p.setColor(QPalette::ButtonText, <your color>);
> combo->setPalette(p);
>
> If you're trying to change the text in the menus, that is a bit problematic
because there is a bug in using the palette to change the text or background in
the menu (i.e., QComboBox::view()) of the combo box.  You can use a style sheet
instead:
>
>     QString styleSheet = "QComboBox   {  color:   <yourcolor>;   }";
>     combo->setStyleSheet(styleSheet);
>
> HTH,
> Susan
>
> P.S.  If you need more detailed help on this, I can provide more detailed
examples.  The app that I am working on has many custom colors so I've had to
set combo box colors not only for active states, but disabled, seletion
(highlight), etc.
>
> ----- Original Message ----
> From: Mark Thompson <mark@xxxxxxxxxxxx>
> To: qt-interest@xxxxxxxxxxxxx
> Sent: Tuesday, April 24, 2007 11:51:50 AM
> Subject: setting text color in a QComboBox
>
> Hi,
> Sorry if this is a lame question; I've dug through the help and can't 
> seem to figure it out.
>
> I want to change the color of the text listed in a QComboBox.
> Is this possible?
>
> Mark
>
>   

-- 
 [ signature omitted ] 
#include <QAbstractItemView>
#include <QCheckBox>
#include <QComboBox>
#include <QPalette>
#include <QStyle>
#include <QVBoxLayout>



class MyCombo: public QComboBox
{
    Q_OBJECT

public:
    enum ColorType {
        PaletteOnly,
        StyleSheetOnly,
        PaletteOnComboAndMenu,
        PaletteOnComboStyleSheetOnMenu
    };
        
    MyCombo(ColorType type, QWidget *parent = NULL)
      :
        QComboBox(parent)
    {
        if (type == PaletteOnly)
        {
            // Only applies palette to the button part of the combo
            //
            QPalette p = palette();
            p.setColor(QPalette::Window,     Qt::red);
            p.setColor(QPalette::WindowText, Qt::white);
            
            p.setColor(QPalette::Button,     Qt::darkCyan);
            p.setColor(QPalette::ButtonText, Qt::white);
            
            p.setColor(QPalette::Disabled, QPalette::Button,     Qt::gray);
            p.setColor(QPalette::Disabled, QPalette::ButtonText, Qt::lightGray);
            
            p.setColor(QPalette::Highlight,       Qt::white);
            p.setColor(QPalette::HighlightedText, Qt::black);
            setPalette(p);
        }
        else if (type == StyleSheetOnly)
        {
            // Notice that the disabled color differs in appearance when
            // using a style sheet versus a palette.  This is also a bug.
            //
            QString styleSheet = "QComboBox \
                {  \
                    background-color:           darkCyan; \
                    color:                      white; \
                    selection-background-color: white; \
                    selection-color:            black; \
                } \
                 \
                QComboBox:disabled  \
                { \
                    background-color:           gray; \
                    color:                      lightGray; \
                }";

            setStyleSheet(styleSheet); 
        }
        else if (type == PaletteOnComboAndMenu || 
            type == PaletteOnComboStyleSheetOnMenu)
        {
            QPalette p = palette();

            // Neither Window/Button work alone, or together...
            //
            p.setColor(QPalette::Window,     Qt::red);
            p.setColor(QPalette::WindowText, Qt::white);
            p.setColor(QPalette::Button,     Qt::darkCyan);
            p.setColor(QPalette::ButtonText, Qt::white);
            
            p.setColor(QPalette::Disabled, QPalette::Button,     Qt::gray);
            p.setColor(QPalette::Disabled, QPalette::ButtonText, Qt::lightGray);
            
            p.setColor(QPalette::Highlight,       Qt::white);
            p.setColor(QPalette::HighlightedText, Qt::black);
            setPalette(p);
            
            // Only applies palette to the button part of the combo, 
            // even when setting it on the popup...
            //
            setPalette(p);
            
            QAbstractItemView *popup = view();
            
            // This works in all styles except cleanlooks
            //
            if (type == PaletteOnComboStyleSheetOnMenu)
            {
                QString styleSheet = "QAbstractItemView \
                    {  \
                        background-color:           darkCyan; \
                        color:                      white; \
                        selection-background-color: white; \
                        selection-color:            black; \
                    }";

                popup->setStyleSheet(styleSheet); 
            }
            else
            {
                popup->setPalette(p);
            }

        }
    }
};



class MyWin: public QWidget
{
    Q_OBJECT

public:
    MyWin(QWidget *parent = NULL)
      :
        QWidget(parent),
        
        _styleCombo  (NULL),
        _paletteCombo(NULL),
        _menuCombo   (NULL)
    {
         QPalette palette;
         palette.setColor(QPalette::Window, QColor(234, 250, 245));
         palette.setColor(QPalette::WindowText, QColor(0, 0, 0));
         setPalette(palette);
         
         QVBoxLayout *layout = new QVBoxLayout(this);
         
         _styleCombo = new MyCombo(MyCombo::StyleSheetOnly, this);
         _styleCombo->addItem("StyleSheetOnly 1");
         _styleCombo->addItem("StyleSheetOnly 2");
         _styleCombo->addItem("StyleSheetOnly 3");
         _styleCombo->addItem("StyleSheetOnly 4");
         layout->addWidget(_styleCombo);
         
         
         _paletteCombo = new MyCombo(MyCombo::PaletteOnly, this);
         _paletteCombo->addItem("PaletteOnly 1");
         _paletteCombo->addItem("PaletteOnly 2");
         _paletteCombo->addItem("PaletteOnly 3");
         _paletteCombo->addItem("PaletteOnly 4");
         layout->addWidget(_paletteCombo);
         
         _menuCombo = new MyCombo(MyCombo::PaletteOnComboAndMenu, this);
         _menuCombo->addItem("PaletteOnComboAndMenu 1");
         _menuCombo->addItem("PaletteOnComboAndMenu 2");
         _menuCombo->addItem("PaletteOnComboAndMenu 3");
         _menuCombo->addItem("PaletteOnComboAndMenu 4");
         layout->addWidget(_menuCombo);
         
         
         _paletteAndStyleCombo = new MyCombo(
            MyCombo::PaletteOnComboStyleSheetOnMenu, this);
         _paletteAndStyleCombo->addItem("PaletteOnComboStyleSheetOnMenu 1");
         _paletteAndStyleCombo->addItem("PaletteOnComboStyleSheetOnMenu 2");
         _paletteAndStyleCombo->addItem("PaletteOnComboStyleSheetOnMenu 3");
         _paletteAndStyleCombo->addItem("PaletteOnComboStyleSheetOnMenu 4");
         layout->addWidget(_paletteAndStyleCombo);
         
         _disable = new QCheckBox("Disable Widgets", this);
         layout->addWidget(_disable);
         
         connect(
            _disable, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));

    }

private slots:
    void stateChanged(int state)
    {
        _paletteCombo        ->setEnabled(state != Qt::Checked);
        _styleCombo          ->setEnabled(state != Qt::Checked);
        _menuCombo           ->setEnabled(state != Qt::Checked);
        _paletteAndStyleCombo->setEnabled(state != Qt::Checked);
    }
    
private:
    MyCombo   *_styleCombo;
    MyCombo   *_paletteCombo;
    MyCombo   *_menuCombo;
    MyCombo   *_paletteAndStyleCombo;
    QCheckBox *_disable;
};

Message 5 in thread

In working with the trolls on these issues, it turns out there is only one real bug.  In cleanlooks style, using the palette on the combo box view to change the menu item text and background doesn't work.  

However for other sytles, setting the palette on the view or the combo box using QPalette::Base and QPalette::Text does indeed work.  Because the docs say that QPalette::Base is used for "the background color for text entry widgets", and QPalette::Text is the foreground color used with Qt::Base., I never even thought to use Base/Text.

So if you want to use a palette exclusively you can:

            QPalette p = combo->palette();

              // Outline around the menu
            p.setColor(QPalette::Window, Qt::red);    
            p.setColor(QPalette::WindowText, Qt::white);

            // combo button
            p.setColor(QPalette::Button, Qt::darkCyan);  
            p.setColor(QPalette::ButtonText, Qt::white);
            
              // combo menu
            p.setColor(QPalette::Base, Qt::darkCyan);  
            p.setColor(QPalette::Text, Qt::white);

            // highlight button & menu
            p.setColor(QPalette::Highlight, Qt::blue);   
            p.setColor(QPalette::HighlightedText, Qt::red);
            
              // to customize the disabled color
            p.setColor(QPalette::Disabled, QPalette::Button, Qt::gray);
            p.setColor(QPalette::Disabled, QPalette::ButtonText, Qt::lightGray);
            
            combo->setPalette(p);

NOTE: In Plastique, if you want the button highlight color (QPalette::Hightlight) to be different from the menu, I haven't found a way to do this.  Setting the highlight colors using the palette on QComboBox::view doesn't seem to work.

FWIW,
Susan

----- Original Message ----
From: Susan Macchia <susan@xxxxxxxxxxxx>
To: Mark Thompson <mark@xxxxxxxxxxxx>
Cc: qt-interest@xxxxxxxxxxxxx
Sent: Tuesday, April 24, 2007 1:38:28 PM
Subject: Re: setting text color in a QComboBox

I have attached a compilable test program that shows all the ways to set the
colors in combo boxes (that I know of).  It also shows the bug trying to use
only QPalette to set the foreground/background colors in the menu of the combo
(the trolls are aware of this bug and are actively working it).

--Susan



----- Original Message ----
From: Mark Thompson <mark@xxxxxxxxxxxx>
To: susan@xxxxxxxxxxxx
Cc: qt-interest@xxxxxxxxxxxxx
Sent: Tuesday, April 24, 2007 12:40:36 PM
Subject: Re: setting text color in a QComboBox

Susan,
Thanks, that helps a lot.
I would certainly like to see a couple of examples.
Mark

susan@xxxxxxxxxxxx wrote:
> Assuming Qt4.
>
> Are you trying to set the text in the button portion or the menu?  If you're
trying to set it in the button portion of the combo box, you need to:
>
> QPalette p = combo->palette()
> p.setColor(QPalette::ButtonText, <your color>);
> combo->setPalette(p);
>
> If you're trying to change the text in the menus, that is a bit problematic
because there is a bug in using the palette to change the text or background in
the menu (i.e., QComboBox::view()) of the combo box.  You can use a style sheet
instead:
>
>     QString styleSheet = "QComboBox   {  color:   <yourcolor>;   }";
>     combo->setStyleSheet(styleSheet);
>
> HTH,
> Susan
>
> P.S.  If you need more detailed help on this, I can provide more detailed
examples.  The app that I am working on has many custom colors so I've had to
set combo box colors not only for active states, but disabled, seletion
(highlight), etc.
>
> ----- Original Message ----
> From: Mark Thompson <mark@xxxxxxxxxxxx>
> To: qt-interest@xxxxxxxxxxxxx
> Sent: Tuesday, April 24, 2007 11:51:50 AM
> Subject: setting text color in a QComboBox
>
> Hi,
> Sorry if this is a lame question; I've dug through the help and can't 
> seem to figure it out.
>
> I want to change the color of the text listed in a QComboBox.
> Is this possible?
>
> Mark
>
>   

-- 
 [ signature omitted ]