QT开发中文网

Qt C++ QTreeWidget 希望将隐藏三角形颜色更改为白色不添加图标 能改变颜色吗?

如果你不想添加图标,而只是改变隐藏的三角形的颜色,可以通过重写QTreeWidget的paintEvent()函数来实现。以下是一个示例代码:

class CustomTreeWidget : public QTreeWidget {
public:
    CustomTreeWidget(QWidget *parent = nullptr) : QTreeWidget(parent) {}

protected:
    void paintEvent(QPaintEvent *event) override {
        QStyleOptionViewItem option;
        option.initFrom(this);

        QPainter painter(viewport());
        const QColor color(Qt::white); // 设置为白色

        for (int i = 0; i < model()->rowCount(rootIndex()); ++i) {
            QModelIndex index = model()->index(i, 0, rootIndex());
            QRect rect = visualRect(index);
            
            // 绘制三角形
            if (!isExpanded(index)) {
                QPoint points[3] = {
                    QPoint(rect.left() + 4, rect.top() + rect.height() / 2 - 3),
                    QPoint(rect.left() + 8, rect.top() + rect.height() / 2),
                    QPoint(rect.left() + 4, rect.top() + rect.height() / 2 + 3)
                };
                painter.setPen(color);
                painter.setBrush(color);
                painter.drawPolygon(points, 3);
            }
        }

        QTreeView::paintEvent(event);
    }
};

使用CustomTreeWidget代替原始的QTreeWidget即可。

这样设置后,隐藏的三角形将会以白色绘制出来。你可以根据需要自行调整三角形的位置和大小,并更改所需的颜色。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=9843

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?