样式QComboBox弹出菜单边距Qt 4

4

在花费了无数小时试图样式化 QComboBox 之后,我遇到了插入项目的顶部和底部间距的困境。

我想要删除或者为弹出菜单的顶部和底部白色空间应用背景颜色。

QComboBox 屏幕截图 http://img576.imageshack.us/img576/3402/screenshot20120130at144.png

我添加了 min-height 到 QListView 中以不显示顶部和底部箭头。我还查看了 Qt 源代码中的 QComboBoxPrivate 类,但似乎上下边距是硬编码的。

感谢您的帮助。

已编辑:这是目前我的样式表

QComboBox {
    border: 1px solid rgb(95, 95, 95);
    border-radius: 3px;
    padding: 1px 18px 1px 3px;
    min-width: 6em;
    color: rgb(220, 220, 220);
    background-color: rgb(80, 80, 80);
    margin: 0 0 0 0;
}

QComboBox:editable {
    background: rgb(80, 80, 80);
}

QComboBox:!editable {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                  stop: 0 rgb(51, 51, 51), stop: 0.4 rgb(39, 39, 39),
                                  stop: 0.5 rgb(32,32,32), stop: 1.0 rgb(38,38,38));;
}

QComboBox::drop-down:editable {
}

QComboBox:!editable:on {
}

QComboBox::drop-down:editable:on {
}

QComboBox:on { /* shift the text when the popup opens */
    padding-top: 3px;
    padding-left: 4px;
    background-color: rgb(80, 80, 80);
}

QComboBox::drop-down {
    subcontrol-origin: padding;
    subcontrol-position: center right;
    width: 15px; 
    right:3px;
    border-top-right-radius: 3px; /* same radius as the QComboBox */
    border-bottom-right-radius: 3px;
    background:none;
}

QComboBox::down-arrow {
    image: url(:/images/arrow-down-inverted.png);
    height: 10px;
}

QComboBox::down-arrow:on { /* shift the arrow when popup is open */
    top: 1px;
    left: 1px;
}

QListView#comboListView {
    background: rgb(80, 80, 80);
    color: rgb(220, 220, 220);
    min-height: 90px;
    margin: 0 0 0 0;
}

QListView#comboListView::item {
    background-color: rgb(80, 80, 80);
}

QListView#comboListView::item:hover {
    background-color: rgb(95, 95, 95);
}

你能发布一下你目前所拥有的样式表吗? - Arnold Spence
这里也有同样的问题,看起来是由于QComboBox的填充引起的。但是在:on状态下将其设置为0似乎没有帮助 :/ - Mariusz Jaskółka
3个回答

6

您需要自定义QComboBox中的QAbstractItemView

QComboBox QAbstractItemView {
    margin-top: 0px;
    padding-bottom: 0px;
}

0

请尝试以下样式表:

QComboBox {
  margin-top: 0px;
  margin-bottom: 0px;
}

这应该将边距设置为0。此外,here是样式表的QT参考。


它设置了QCombobox的边距,而不是菜单内部的边距。 - Kirell

0
我找到的解决方法是将组合框设置为只读,这样就可以去除白色边距了。
QCombobox* choiceBox = new QCombobox;
choiceBox->lineEdit()->setReadOnly(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接