Qt 4 QComboBox弹出菜单边距样式

发布于 2024-12-29 19:19:25 字数 1970 浏览 1 评论 0原文

经过无数个小时尝试设计 QComboBox 的样式后,我被插入项目的顶部和底部边距所困扰。

我想删除或应用背景颜色到弹出菜单顶部和底部空白。

QComboBox 屏幕 http://img576.imageshack.us/img576/3402/screenshot20120130at144.png< /a>

我添加了一个最小高度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);
}

After countless hours trying to style a QComboBox, I'm stuck with the top and bottom margin where the items are inserted.

I would like to either remove or apply a background-color to the popup menu top and bottom white spaces.

QComboBox screen http://img576.imageshack.us/img576/3402/screenshot20120130at144.png

I added a min-height to the QListView not to show top and bottom arrows. I also looked at the QComboBoxPrivate class in Qt sources but it seems that the top and bottom margins are hardcoded.

Thanks for your help.

Edited: This is my stylesheet so far

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彩虹直至黑白 2025-01-05 19:19:25

您需要在QComboBox中自定义QAbstractItemView

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

You need to customize the QAbstractItemView in the QComboBox:

QComboBox QAbstractItemView {
    margin-top: 0px;
    padding-bottom: 0px;
}
瀟灑尐姊 2025-01-05 19:19:25

尝试以下样式表:

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

这应该将边距设置为 0。另外此处 是对样式表的 qt 引用。

Try the following stylesheet:

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

This should set the margins to 0. Also here is a qt reference to the stylesheets.

岁月蹉跎了容颜 2025-01-05 19:19:25

我发现删除白边的解决方案是将组合框设置为只读。

QCombobox* choiceBox = new QCombobox;
choiceBox->lineEdit()->setReadOnly(true);

The solution I found to remove the white margin is to set the combobox to read-only.

QCombobox* choiceBox = new QCombobox;
choiceBox->lineEdit()->setReadOnly(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文