【问题标题】:Style hover and selected colors for arbitrarely chosen QTreeWidgetItems任意选择的 QTreeWidgetItems 的样式悬停和选定颜色
【发布时间】:2015-01-25 01:08:50
【问题描述】:

我有一个QTreeWidget,我有一个样式表应用到它。我希望某些QTreeWidgetItems 具有与其他样式表样式项目不同的hoverselected 颜色。我用setData(columnNumber, Qt::ForegroundRole, colorName)normal 状态着色,但我无法更改悬停和选定状态的颜色。

有谁知道是否有可能以某种方式在Qt 中实现这一目标?

谢谢!

【问题讨论】:

    标签: qt stylesheet qtreewidget qtreewidgetitem qstyle


    【解决方案1】:

    AFAIK 样式表并不是万能的。你想要非常具体的东西,所以你应该更深入地研究并使用更强大的东西。我建议你使用委托。你没有提供规范,所以我提供主要思想。在QStyledItemDelegate 子类中重新实现paint。例如:

    void ItemDelegatePaint::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QString txt = index.model()->data( index, Qt::DisplayRole ).toString();
    
        if( option.state & QStyle::State_Selected )//it is your selection
        {
            if(index.row()%2)//here we try to see is it a specific item
                painter->fillRect( option.rect,Qt::green );//special color
            else
                painter->fillRect( option.rect, option.palette.highlight() );
            painter->drawText(option.rect,txt);//text of item
        } else
        if(option.state & QStyle::State_MouseOver)//it is your hover
        {
            if(index.row()%2)
                painter->fillRect( option.rect,Qt::yellow );
            else
                painter->fillRect( option.rect, Qt::transparent );
            painter->drawText(option.rect,txt);
        }
        else
        {
            QStyledItemDelegate::paint(painter,option,index);//standard process
        }
    
    }
    

    这里我为每个第二个项目设置了一些特定的属性,但你可以使用另一个特定的项目。

    QTreeWidget 继承 QTreeView 所以使用:

    ui->treeWidget->setItemDelegate(new ItemDelegatePaint);
    

    看来你的小部件很复杂,所以我希望你理解主要思想,你将能够编写绝对适合你的委托。如果您之前没有使用过委托,那么请查看示例,这并不是很复杂。

    http://qt-project.org/doc/qt-4.8/itemviews-stardelegate-stardelegate-h.html

    http://qt-project.org/doc/qt-4.8/itemviews-stardelegate-stardelegate-cpp.html

    在我的回答中,我使用了下一个代表:

    #ifndef ITEMDELEGATEPAINT_H
    #define ITEMDELEGATEPAINT_H
    
    #include <QStyledItemDelegate>
    class ItemDelegatePaint : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        explicit ItemDelegatePaint(QObject *parent = 0);
        ItemDelegatePaint(const QString &txt, QObject *parent = 0);
    
    
    protected:
        void paint( QPainter *painter,
                    const QStyleOptionViewItem &option,
                    const QModelIndex &index ) const;
        QSize sizeHint( const QStyleOptionViewItem &option,
                        const QModelIndex &index ) const;
        QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        void setEditorData(QWidget * editor, const QModelIndex & index) const;
        void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
        void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    
    signals:
    
    public slots:
    
    };
    
    #endif // ITEMDELEGATEPAINT_H
    

    这里有很多方法,但paint对你来说是最重要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      相关资源
      最近更新 更多