簡體   English   中英

如何在Qt中按區域填充不同顏色段的圓角矩形?

[英]How to fill a rounded rectangle with different color segments by area in Qt?

我是Qt的新手,我嘗試在網上查找示例和文檔,但找不到任何東西。 我想要這樣的東西:

在此輸入圖像描述

我嘗試使用QLinearGradient,但它不是我想要的。 我想要純色。 這是我嘗試過的:

void drawBackground ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
    QLinearGradient linearGrad(QPointF(option.rect.x(), 0), QPointF(option.rect.x() + option.rect.width(), 0));

    int total = index.data(StatisticsModel::TotalCount).toInt();
    linearGrad.setColorAt(0.0, QColor(255, 255, 255, 0));

    int sum = 0;
    for (int i = 7; i >= 1; i--) {
        int count = index.data(StatisticsModel::Grade0 + i).toInt();

        if (count) {
            sum += count;
            linearGrad.setColorAt(1.0-((double)(total-sum))/total, Prefs::gradeColor(i));

        }
    }


    QRect rect(option.rect);
    rect.adjust(1, 1, -1, -1);

    QPainterPath path;
    path.addRoundedRect( rect, 2.0, 2.0 );
    painter->setBrush(QBrush(linearGrad));
    painter->drawPath(path);
}

任何幫助,將不勝感激。

那么像這樣的彩色圓角矩形的最佳方法我想是為它創建QPainterPath然后構造正常的矩形,它應該是指定顏色與初始圓角矩形QPainterPath QPainterPath::intersected ,使用函數QPainterPath::intersected並繪制它們,選擇相應的純色刷和使用函數drawPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM