【问题标题】:How can I set the header color of a combo box?如何设置组合框的标题颜色?
【发布时间】:2016-05-19 16:44:34
【问题描述】:

我想做一个组合框来选择一些与颜色相关的内容。我希望内容的背景显示颜色。我已经做到了:

QList<QString> names;
QList<QColor> bgColors;
QList<QColor> fgColors;
QComboBox* colorComboBox = new QComboBox();
for(int i = 0; i < names.size(); ++i)
{
    colorComboBox->addItem(names.at(i), bgColors.at(i));
    const QModelIndex idx = colorComboBox->model()->index(i, 0);
    colorComboBox->model()->setData(idx, bgColors.at(i), Qt::BackgroundColorRole);
    colorComboBox->model()->setData(idx, fgColors.at(i), Qt::ForegroundRole);
}

组合框显示我想要的文本,以及我想要的背景颜色(不像 ColorEditorFactory 示例那样精致,它只在文本旁边显示一个小矩形,但这就是我想要的方式)。

我需要什么:

一旦选择了行/颜色,我希望组合框显示颜色。与现在一样,组合框在关闭时显示文本但不显示颜色。

如何更改组合框标题的颜色? (我称它为标题,但它可能有不同的名称,不确定 - 显示在表格上方的部分用于选择和组合框关闭时)

编辑:我试图在currentIndexChanged 的插槽中设置样式表:

setStyleSheet("QComboBox { color: " + fgColor +
             "; background-color: " + bgColor + "; }");

结果:它改变了整个组合框的颜色,忘记了初始颜色。

setStyleSheet("QComboBox:!on { color: " + fgColor +
             "; background-color: " + bgColor + "; }");

结果:它在未选择时很好地改变了颜色 - 但突出显示和标题是灰色的并且难以阅读,我希望我也可以改变它。当我悬停时,整个组合颜色会变为我设置的最后一个颜色。

答案可能在样式表中——如果我能找出适用于标题的属性。

【问题讨论】:

    标签: qt colors combobox stylesheet qmodelindex


    【解决方案1】:

    如果我理解得很好,您希望在单击项目后立即更改 QComboBox 标题的颜色。我有这段代码对我有用:

    在 .cpp 文件中:

    myClass::init() // called in the constructor of myClass
    {
        names.clear();
        names.append("One");
        names.append("Two");
        names.append("Three");
    
        bgColors.clear();
        bgColors.append(QColor("blue"));
        bgColors.append(QColor("red"));
        bgColors.append(QColor("green"));
    
        fgColors.clear();
        fgColors.append(QColor("yellow"));
        fgColors.append(QColor("magenta"));
        fgColors.append(QColor("cyan"));
    
        colorComboBox = new QComboBox();
        colorComboBox->setItemDelegate(new SelectionKillerDelegate);
    
        // don't know why but this line seems important; uncomment it,
        // and the text in the QComboBox (not the items) will be highlighted
        colorComboBox->setStyleSheet("QComboBox { border-radius: 1px; }");
    
        for(int i = 0; i < names.size(); ++i)
        {
            colorComboBox->addItem(names.at(i), bgColors.at(i));
            const QModelIndex idx = colorComboBox->model()->index(i, 0);
            colorComboBox->model()->setData(idx, bgColors.at(i), Qt::BackgroundColorRole);
            colorComboBox->model()->setData(idx, fgColors.at(i), Qt::ForegroundRole);
        }
    
        connect(colorComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxChanged(int)));
    }
    
    
    void myClass::comboBoxChanged(int index)
    {
        QColor forecolor = (QColor) (colorComboBox->itemData(index, Qt::ForegroundRole).value<QColor>());
        QString fgColor = forecolor.name(QColor::HexRgb);
    
        QColor backcolor = (QColor) (colorComboBox->itemData(index, Qt::BackgroundRole).value<QColor>());
        QString bgColor = backcolor.name(QColor::HexRgb);
    
        setStyleSheet("QComboBox { color: " + fgColor + "; background-color: " + bgColor + "; }");
    
    }
    

    在 .h 文件中(感谢 this answerQItemDelegate 子类,它允许您选择 QComboBox 的项目但不突出显示):

    class SelectionKillerDelegate : public QItemDelegate
    {
        virtual void paint(QPainter *painter,
            const QStyleOptionViewItem &option,
            const QModelIndex &index) const override
         {
             QStyleOptionViewItem myOption = option;
             myOption.state &= (~QStyle::State_Selected);
             QItemDelegate::paint (painter, myOption, index);
         }
     };
    
    private : 
        QComboBox* colorComboBox;
        QList<QString> names;
        QList<QColor> bgColors;
        QList<QColor> fgColors;
    
    public slots :
        void comboBoxChanged(int);
    

    我希望它也对你有用。

    【讨论】:

    • 这太棒了!我最终让它与样式表一起工作,但我认为这更容易。我只是无法理解setStyleSheet("QComboBox { border-radius: 1px; }"); 的要求——因为无论如何它都会在更改槽中被覆盖......
    • 我也没有,但你注意到和我一样的吗?如果您对此行发表评论,则 QComboBox 文本会再次突出显示...我还没有能力理解这种行为哈哈
    • QComboBox 中的文本?对我来说仍然突出显示 - 现在你提到了它,我将它插入到插槽样式表中,它删除了突出显示! :-) 再次感谢 :-)
    • 这很有趣 - 似乎我真正需要的是边框半径 - ItemDelegate 子类只会删除列表视图上突出显示的状态,我可能想要也可能不想要(我会看看人们如何喜欢它,很高兴有选择)。这肯定是一个错误......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多