QcomboBox item style

The solution is to

  • create a ListView object
  • set its stylesheet
  • use it as the view of the ComboBox

Here is how:

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QMainWindow * mainWindow = new QMainWindow();
    QComboBox * combo = new QComboBox(mainWindow);
    QListView * listView = new QListView(combo);
    combo->addItem("foo");
    combo->addItem("bar");
    combo->addItem("foobar");
    combo->addItem("fooooo");

    listView->setStyleSheet("QListView::item {                              \
                             border-bottom: 5px solid white; margin:3px; }  \
                             QListView::item:selected {                     \
                             border-bottom: 5px solid black; margin:3px;    \
                             color: black;                                  \
                            }                                               \
                            ");
    combo->setView(listView);


    mainWindow->show();
    app.exec();

    return 0;
    }

Remark: I think, according to the Qt docs applying this style should also work...but it does not.

QComboBox QAbstractItemView::item {
    border-bottom: 5px solid white; margin:3px;
}
QComboBox QAbstractItemView::item:selected {
    border-bottom: 5px solid black; margin:3px;
}
發佈了49 篇原創文章 · 獲贊 49 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章