3

この質問は、この投稿をさらに発展させたものであり、これと似ているように見えるかもしれませんが、異なります。

QHeaderView::paintSectionモデルから返された背景が尊重されるように、を再実装しようとしています。私はこれをやろうとしました

void Header::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
    QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
    // try before
    if(bg.isValid())                // workaround for Qt bug https://bugreports.qt.io/browse/QTBUG-46216
        painter->fillRect(rect, bg.value<QBrush>());             
    QHeaderView::paintSection(painter, rect, logicalIndex);
    // try after
    if(bg.isValid())                // workaround for Qt bug https://bugreports.qt.io/browse/QTBUG-46216
        painter->fillRect(rect, bg.value<QBrush>());             
}

しかし、それはうまくいきませんでした-私がQHeaderView::paintSection電話をかけた場合、ペインターで描いたものは何も見えません(対角線も描いてみました)。通話を削除するQHeaderView::paintSectionと、線と背景が表示されます。の前と後にfillRect呼び出しQHeaderView::paintSectionを行っても、違いはありません。

QHeaderView::paintSectionその上に何かを描くことができないのは何なのだろうか。そして、すべてを再実装せずにそれを克服する方法QHeaderView::paintSectionはありますか?

私がする必要があるのは、特定のセルに特定の色合いを追加することだけです-セル内のすべて(テキスト、アイコン、グラデーションの背景など)をそのままペイントしたいのですが...

4

1 に答える 1

8

It is obvious why the first fillRect doesn't work. Everything that you paint before paintSection is overridden by base painting.

The second call is more interesting.

Usually all paint methods preserves painter state. It means that when you call paint it looks like the painter state hasn't been changed.

Nevertheless QHeaderView::paintSection spoils the painter state.

To bypass the issue you need to save and restore the state by yourself:

void Header::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
    QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();
    if(bg.isValid())               
        painter->fillRect(rect, bg.value<QBrush>());             
}
于 2015-06-17T09:52:36.650 に答える