QcomboBox item style

本文介绍如何使用Qt自定义ComboBox的下拉列表样式,通过创建ListView对象并设置样式表来实现。示例代码展示了如何为ComboBox的每个项设置边框和选中状态样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值