1

QStyledItemDelegateのアイテムのスタイルを設定するために使用していQTreeViewます。

私のツリービューのルートは装飾されていません。以下のような関係を持つ単純なツリーです。

ColorBook1
    Color1
    Color2
ColorBook2
    Color3

親と子のスタイルが異なり、親の選択が無効になっています。

子ノードの選択動作をカスタマイズして、子の周りの選択長方形がテキストの子だけではなく行全体をカバーするようにしたいと考えています。

現在の動作:

ここに画像の説明を入力

望ましい動作:

ここに画像の説明を入力

を使用して、このように選択長方形を拡張する方法はありますQStyledItemDelegateか? adjustingrectinQStyleOptionViewItemパラメータを試してみましたQStyledItemDelegate::paint。しかし、それは子ノードのテキストを左に移動しました。テキストノードを同じ場所に保持したいのですが、選択長方形だけを左に調整する必要があります。したがって、ペイント メソッドでテキストとピックスマップを描画するのと同じように、選択長方形も描画する方法があります (既定の選択長方形の色を使用)。

私の StyledItemDelegate のペイント方法は次のとおりです。

QStyledItemDelegate::paint メソッドで次のコードを使用しています。

void paint( QPainter * inPainter, const  QStyleOptionViewItem & inOption, const QModelIndex & inIndex ) const
  {
   if( inIndex.data( Qt::UserRole ) == ColorInfoType::kColorBook )
   {
      QFont font = inPainter->font();
      font.setWeight( QFont::Bold );
      font.setPointSize( 8 );
      inPainter->setFont( font );
      inPainter->drawText
         (
            inOption.rect.adjusted( 5,0,0,0 ),
            inIndex.data( Qt::DisplayRole ).toString(),
            QTextOption( Qt::AlignVCenter | Qt::AlignLeft )
         );
   }
   else
   {
      //To Do: draw the selection rect after adjusting the size.
      // Draw the Color Name text
      QStyledItemDelegate::paint( inPainter, inOption, inIndex );
   }
  }
4

1 に答える 1