質問

これがストーリーです:

QSqlQueryModelを使用してそれを埋めるQListviewがあります。一部の項目はモデルの非表示列の値に基づいて太字で表示されるため、独自のカスタムデリゲートを作成することにしました。私はPyQT 4.5.4を使用しているため、ドキュメントに従ってQStyledItemDelegateから継承する方法です。動作しましたが、いくつかの問題があります。

ここに私の解決策があります:

class TypeSoortDelegate(QStyledItemDelegate):

    def paint(self, painter, option, index):
        model = index.model()
        record = model.record(index.row())
        value= record.value(2).toPyObject()
        if value:
            painter.save()
            # change the back- and foreground colors
            # if the item is selected
            if option.state & QStyle.State_Selected:
                painter.setPen(QPen(Qt.NoPen))
                painter.setBrush(QApplication.palette().highlight())
                painter.drawRect(option.rect)
                painter.restore()
                painter.save()
                font = painter.font
                pen = painter.pen()
                pen.setColor(QApplication.palette().color(QPalette.HighlightedText))
                painter.setPen(pen)
            else:
                painter.setPen(QPen(Qt.black))

            # set text bold
            font = painter.font()
            font.setWeight(QFont.Bold)
            painter.setFont(font)
            text = record.value(1).toPyObject()
            painter.drawText(option.rect, Qt.AlignLeft, text)

            painter.restore()
        else:
            QStyledItemDelegate.paint(self, painter, option, index)

今直面している問題:

  1. 通常の(太字ではない)アイテムは わずかにインデントされています(数ピクセル)。 これはおそらくいくつかのデフォルトです 動作。アイテムをインデントできます 太字も、しかし何が起こるか 別のプラットフォームの下で?
  2. 通常、アイテムを選択すると、点線で囲まれた小さな境界線があります(Windowsの既定の設定?)。ここでも私はそれを描くことができましたが、私はできるだけネイティブのままにしたいです。

次の質問:

特定の条件が満たされた場合にのみフォントの太さを変更し、残りはすべてそのままにするカスタムデリゲートを作成する別の方法はありますか?

私も試しました:

if value:
    font = painter.font()
    font.setWeight(QFont.Bold)
    painter.setFont(font)
QStyledItemDelegate.paint(self, painter, option, index)

しかし、それは外観にまったく影響を与えないようです。エラーはなく、デフォルトの動作であり、太字の項目はありません。

すべての提案を歓迎します!

役に立ちましたか?

解決

これはテストしていませんが、できると思います:

class TypeSoortDelegate(QStyledItemDelegate):

def paint(self, painter, option, index):
    get value...
    if value:
        option.font.setWeight(QFont.Bold)

    QStyledItemDelegate.paint(self, painter, option, index)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top