改变下拉列表的样式

  1. // 1. 添加代理
  2. QStyledItemDelegate* styleItemDelegate = new QStyledItemDelegate();
  3. ui->comboBox->setItemDelegate(styleItemDelegate);
  4. // 2. qss样式
  5. QComboBox {
  6. border: 1px solid rgb(208, 211, 211); /* 边框 */
  7. color: rgba(0, 0, 0, 255); /* 字体颜色 */
  8. background-color: rgb(241, 244, 244); /* 背景色 */
  9. }
  10. QComboBox QAbstractItemView {
  11. border: 1px solid darkgray; /* 下拉列表边框 */
  12. selection-background-color: lightgray; /* 下拉列表选项选中的颜色 */
  13. padding-top:2px; /* 下拉列表距顶端和底端的距离 */
  14. padding-bottom:2px;
  15. }
  16. QComboBox QAbstractItemView::item{
  17. height: 20px; /* 设置下拉列表选项的行高 */
  18. }
  1. QComboBox {
  2. border: 1px solid #bebebe;
  3. padding: 1px 18px 1px 3px;
  4. font: normal normal 16px "Microsoft YaHei";
  5. color: #555555;
  6. background: transparent;
  7. }
  8. QComboBox:editable{
  9. background: transparent;
  10. }
  11. QComboBox:!editable, QComboBox::drop-down:editable{
  12. background: transparent;
  13. }
  14. QComboBox:!editable:on, QComboBox::drop-down:editable:on{
  15. background: transparent;
  16. }
  17. QComboBox:!on{
  18. }
  19. QComboBox:on{ /* the popup opens */
  20. color: #555555;
  21. border-color: #327cc0;
  22. background: transparent;
  23. }
  24. QComboBox::drop-down{
  25. subcontrol-origin: padding;
  26. subcontrol-position: top right;
  27. width: 20px;
  28. border-left-width: 1px;
  29. border-left-color: darkgray;
  30. }
  31. QComboBox::down-arrow {
  32. image: url(:/common/combobox_arrow);
  33. }
  34. QComboBox::down-arrow:on {
  35. image: url(:/common/combobox_arrow_up);
  36. }
  37. QComboBox QAbstractItemView {
  38. outline: 0;
  39. border: 1px solid #327cc0;
  40. background-color: #F1F3F3;
  41. font: normal normal 14px "Microsoft YaHei";
  42. }
  43. QComboBox QAbstractItemView::item {
  44. height: 32px;
  45. color: #555555;
  46. background-color: transparent;
  47. }
  48. QComboBox QAbstractItemView::item:hover {
  49. color: #FFFFFF;
  50. background-color: #327cc0;
  51. }
  52. QComboBox QAbstractItemView::item:selected {
  53. color: #FFFFFF;
  54. background-color: #327cc0;
  55. }
  56. QComboBox QAbstractScrollArea QScrollBar:vertical {
  57. background-color: #d0d2d4;
  58. }
  59. QComboBox QAbstractScrollArea QScrollBar::handle:vertical {
  60. background: rgb(160,160,160);
  61. }
  62. QComboBox QAbstractScrollArea QScrollBar::handle:vertical:hover {
  63. background: rgb(90, 91, 93);
  64. }

注意:

  1. QCombox *box = new QComboBox();
  2. box->setView(new QListView());