在mac和linux的QComboBox中交替行颜色

alternating row colors in a QComboBox in mac and linux

本文关键字:颜色 QComboBox mac linux      更新时间:2023-10-16

我想替换QComboBox的颜色。在Windows中,我使用view(). setalternatingrowcolors (true)函数没有问题。在Linux和Mac中,这似乎是不可能的。我也尝试使用样式表(见下面的代码),但我有相同类型的结果(所有行具有相同的背景颜色)。你能解释一下我的错误是什么吗?

#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setStyleSheet("QComboBox QAbstractItemView{qproperty-alternatingRowColors: true;alternate-background-color: blue;background: red;}");
    QComboBox b;
    b.addItem("MM_NONE");
    b.addItem("MM_VERT");
    b.addItem("MM_FACE");
    b.addItem("MM_EDGE");
    bool tt = false;
    tt = b.view()->alternatingRowColors();
    b.show();
    return a.exec();
}

至少在我的盒子上,QPalette::BaseQPalette::AlternateBase似乎是相同的颜色:)将QPalette::AlternateBase更改为其他颜色使此代码工作良好:

#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
#include <QPalette>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QComboBox b;
    b.view()->setAlternatingRowColors(true);
    QPalette p = b.palette();
    p.setColor(QPalette::AlternateBase, Qt::red);
    b.setPalette(p);
    b.addItem("MM_NONE");
    b.addItem("MM_VERT");
    b.addItem("MM_FACE");
    b.addItem("MM_EDGE");
    b.show();
    return a.exec();
}