QComboBox drop-down list - set selected item style

后端 未结 3 1302
走了就别回头了
走了就别回头了 2021-01-12 00:49

Is it possible to set selected item style (Qt style sheet) of the QComboBox drop-down list?

3条回答
  •  难免孤独
    2021-01-12 01:22

    If you mean you want the selected item to appear different when the combo box is showing its elements (i.e. in the "dropped down" state), you can change the colors for the Highlight and HighlightedText in the palette, or style the inner QAbstractItemView

    #include 
    
    int main(int argc, char **argv) {
      QApplication app(argc, argv);
    
      QComboBox cb;
      cb.addItem("Item 1");
      cb.addItem("Item 2");
      cb.addItem("Item 3");
      cb.show();
    
      QPalette p = cb.palette();
      p.setColor(QPalette::HighlightedText, QColor(Qt::red));
      p.setColor(QPalette::Highlight, QColor(Qt::green));
      cb.setPalette(p);
    
      // OR ...
      // cb.setStyleSheet("QComboBox QAbstractItemView { "
      //                  "selection-background-color: green; "
      //                  "selection-color: red; }");
    
      return app.exec();
    }
    

    If you just mean the style of the element in its "collapsed" state, I'd take a look at the "Customizing QComboBox" section of the Qt Style Sheets reference for examples on what you are trying to do.

提交回复
热议问题