Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCombo selected item background colour



  • I have a drop-down combo.
    I can set the background colour of an item.
    However, when this item is selected, the background changes some mid-gray. Once another item is selected, the background colour returns to the one I set.

    I don't want to set the selected-item colour but want to display the pre-set colour even if it is selected.
    I have no idea how to go about this.
    Also, there are many combo boxes, I need to set all of them do this.

    Thanks,
    Bill

    Qt 5.13.1.
    KDE Neon.



  • @Bill52
    So how do you set the background color of an item? You need to do the same when an item is selected.



  • @Bill52 said in QCombo selected item background colour:

    Also, there are many combo boxes, I need to set all of them do this.

    Hi,
    If I understood correctly, I have a solution that I use as follows.
    previously added but now deleted data from the database will be shown in red.

    QString DataModul::setComboItem(QString sId, QString sValue, QComboBox *sender)
    {
        bool bMatched = false;
        for(int i = 0; i < sender->count(); i++) {
            if(sender->itemText(i) == sValue) {
                bMatched = true;
                break;
            }
        }
        if(!bMatched) {
            sender->insertItem(1, sValue, sId);
            sender->setItemData(1, QBrush(QColor::fromRgb(255, 200, 200)), Qt::BackgroundRole);
        }
        return sValue;
    }
    
    void DataModul::comboDoldur(QComboBox *combo, QString table, int typeId)
    {
        int index = combo->currentIndex();
        combo->blockSignals(true);
        combo->clear();
        
        // bla bla bla
        
         combo->addItem("", ""); // data bilgisini ekliyelim...
         while(qFonk->next())
    		setComboItem(qFonk->value("ID").toString(), qFonk->value(sValue).toString(), combo);
    		
    	// bla bla bla
    		
        combo->setCurrentIndex(index);
        combo->blockSignals(false);
    }
    
    // usage 
       comboDoldur(ui->comboHayvanIrk, "tur", IRK); // #define IRK 1
    
    


  • @JonB said in QCombo selected item background colour:

    @Bill52
    So how do you set the background color of an item? You need to do the same when an item is selected.

    Thanks for the reply.
    Below code attempting to do that.
    Doesn't work, the selected item is still gray. Something is temporarily overriding the BG colour. Is there some other way to set it?

    I'm wondering if this behaviour originates from within Qt or it is inherited from the OS.
    Would it be too much to ask, if you are on Windows, run a test if the behaviour is the same?
    Many thanks...

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QStringList comboitems;
        comboitems << "Item 1" << "Item 2" << "Item 3";
    
        ui->comboBox->addItems(comboitems);
        ui->comboBox->setItemData(0,QColor(Qt::blue),Qt::BackgroundRole);
    }
    
    void MainWindow::on_comboBox_activated(int index)
    {
        ui->comboBox->setItemData(index,QColor(Qt::blue),Qt::BackgroundRole);
    }
    


  • @Bill52 said in QCombo selected item background colour:

    QCombo selected item background colour:

    This question seems to have been asked before on the web. What about using stylesheet (instead of all that code)? https://stackoverflow.com/a/47021133 claims to be Qt 5.7+, or you can try the others?

    No, I don't have Windows Qt.


  • Lifetime Qt Champion

    Hi
    Qt::BackgroundRole is the actual background. (at all times)
    If you select an item, it will be colored with the selection color/style/platform way until some other
    item is selected. Is that what you are seeing?



  • @mrjj
    I think the question/claim is: a selected item seems to have a "selection" color rather than the non-selected background color, how can user override that as he prefers the non-selected background color he has specified to that used when selected?


  • Lifetime Qt Champion

    @JonB
    Heh. i had to read that twice :)
    Basically he wants No extra "highlighting" when selecting items?



  • @mrjj
    His specification is

    I can set the background colour of an item.

    However, when this item is selected, the background changes some mid-gray. Once another item is selected, the background colour returns to the one I set.

    I don't want to set the selected-item colour but want to display the pre-set colour even if it is selected.

    He wants the background colour --- which he has already changed to his liking on non-selected items --- to stay as it was when an item is selected, rather than (at present) where it changes to some other color.

    He has said nothing other than that about "highlighting", so e.g. if selecting changes the foreground color or boldness (I don't know if it does) we don't care, it's the background color which matters.

    :)

    [Could have gotten it done by now by just using stylesheet ;-) ]


  • Lifetime Qt Champion

    @JonB said in QCombo selected item background colour:

    [Could have gotten it done by now by just using stylesheet ;-) ]

    Hehe indeed.
    I was mostly asking as i didnt understand completely but
    a stylesheet can both remove or use the same color for "highlighting"



  • @JonB said in QCombo selected item background colour:

    @mrjj
    His specification is

    I can set the background colour of an item.

    However, when this item is selected, the background changes some mid-gray. Once another item is selected, the background colour returns to the one I set.

    I don't want to set the selected-item colour but want to display the pre-set colour even if it is selected.

    He wants the background colour --- which he has already changed to his liking on non-selected items --- to stay as it was when an item is selected, rather than (at present) where it changes to some other color.

    He has said nothing other than that about "highlighting", so e.g. if selecting changes the foreground color or boldness (I don't know if it does) we don't care, it's the background color which matters.

    :)

    Thanks for the reply, spot on!

    [Could have gotten it done by now by just using stylesheet ;-) ]

    Could have if i just knew WHAT to actually type in ... if you'd like to quickly type it out ...?

    What I checked is:
    https://doc-snapshots.qt.io/qt5-dev/stylesheet-examples.html#customizing-qcombobox
    It has some haphazard examples, none seem to be relevant.

    Back to the core documentation:
    https://doc-snapshots.qt.io/qt5-dev/stylesheet-reference.html#list-of-properties
    where it says about 1/4 way down the page:"
    List of Properties
    The table below lists all the properties supported by Qt Style Sheets. "

    Here I found a relevant property: selection-background-color
    Which says "The background of selected text or items."
    Except that it does not. It temporarily changes the background color of the item the mouse is over.

    Another relevant property is : background.
    It says "Shorthand notation for setting the background. Equivalent to specifying background-color"
    Except that not entirely. It does change the BG colour but it also changes the 'highlight' colour to the same. Highlight=the temporary change of the item's BG colour while the mouse is over it.
    To overcome this problem, the highligt-BG-color must be reset too every time.
    Highlight-BG-colour here means : selection-background-color. See code below.

    I am still wondering, how can the 'selection-background-color' retrieved from the stylesheet-mess.
    Reading:
    https://doc.qt.io/qt-5/qt.html
    about 2/3 down says: "Roles describing appearance and meta data (with associated types):"
    In the following list i couldn't find any relevant role to get the current 'selection-background-color'.
    Also, so far I couldn't find anything on google.

    If you feel that i am totally lost and just about randomly shooting in the dark: you are not wrong. That is exactly what i feel.
    If someone could give a more intelligent code, it would be appreciated.

    This is the code that almost works:
    (For clarity's sake, irrelevant code is omitted)

    some-function()
    {
        QStringList comboitems;
        comboitems << "Item 1" << "Item 2" << "Item 3";
    
        ui->comboBox->addItems(comboitems);
        ui->comboBox->setItemData(0,QColor(Qt::blue),Qt::BackgroundRole);
        ui->comboBox->setItemData(1,QColor(Qt::red),Qt::BackgroundRole);
        ui->comboBox->setItemData(2,QColor(Qt::yellow),Qt::BackgroundRole);
    }
    
    void MainWindow::on_comboBox_activated(int index)
    {
    /*one can't simply currentStyle = combo.getStylesheet because these are cascading and the object only knows 
        about the result. It is unable to return the source. It can return the stylesheet if you set one. Welcome to the beauty of the styleshit.
        */
    
        //get the current BG colour
        QVariant var = (ui->comboBox->itemData(index, Qt::BackgroundRole));
    
        //convert to QColor
        QColor mycolor = var.value<QColor>();
    
        //there is no built-in function to convert QColor to a string ... but the styleshit needs a string
        int red   = mycolor.red();
        int green = mycolor.green();
        int blue  = mycolor.blue();
        QString strRGB = "rgb(" + QString::number(red) + "," + QString::number(green)+ "," + QString::number(blue) + ")";
    
        qDebug() << "strRGB: " << strRGB;
        //the current selection-beckground-color shoud be retrieved but i have no idea how to do this. Thus it is hard-coded.
        //... and FINALLY!
        //Set the BG colour AND reset the selection-highlight colour
        ui->comboBox->setStyleSheet("background:" + strRGB + "; selection-background-color: yellow;");
    
    }
    

Log in to reply