1

paint()の関数を再実装QTreeWidgetしました。2 列目のデータを太字で表示したいのですが、うまくいきません。

どうすれば修正できますか?

void extendedQItemDelegate::paint(QPainter *painter,
                                  const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
{
    const QString txt = index.data().toString();
    painter->save();
    QFont painterFont;
    if (index.column() == 1) {
        painterFont.setBold(true);
        painterFont.setStretch(20);
    }
    painter->setFont(painterFont);
    drawDisplay(painter, option, rect, txt);
    painter->restore();
}

問題のスクリーンショットを添付しました。後半は太字にする必要があります

スクリーンショット

4

2 に答える 2

0

メンバー関数を介しextendedQItemDelegateQTreeView/QTreeWidgetオブジェクトに追加するのを忘れました。setItemDelegate

例として:

QTreeWidget* tree_view = ...;
extendedQItemDelegate* extended_item_delegate = ...;
tree_view->setItemDelegate(extended_item_delegate);
于 2014-03-15T11:38:48.613 に答える
0

のコピーを作成し、そのコピーにフォントの変更を適用してから、関数に渡されたconst QStyleOptionViewItem &optionオリジナルの代わりにコピーを使用してペイントする必要があります。option

void extendedQItemDelegate::paint(QPainter *painter,
                                  const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
{
    const QString txt = index.data().toString();
    painter->save();
    QStyleOptionViewItem optCopy = option;     // make a copy to modify
    if (index.column() == 1) {
        optCopy.font.setBold(true);            // set attributes on the copy
        optCopy.font.setStretch(20);
    }
    drawDisplay(painter, optCopy, rect, txt);  // use copy to paint with
    painter->restore();
}

qt(これは古い質問ですが、タグの上部にポップアップしていたことに気付きました。)

于 2017-12-19T02:10:16.943 に答える