カスタムQStyledItemDelegateを設定しているQTableViewがあります。
カスタムアイテムのペイントに加えて、選択/ホバー状態の行の背景色のスタイルを設定したいと思います。私が探している外観は、次のKGetスクリーンショットのようなものです 。KGetの行の背景http://www.binaryelysium.com/images/kget_background.jpeg
これが私のコードです:
void MyDelegate::paint( QPainter* painter, const QStyleOptionViewItem& opt, const QModelIndex& index ) const
{
QBrush backBrush;
QColor foreColor;
bool hover = false;
if ( opt.state & QStyle::State_MouseOver )
{
backBrush = opt.palette.color( QPalette::Highlight ).light( 115 );
foreColor = opt.palette.color( QPalette::HighlightedText );
hover = true;
}
QStyleOptionViewItemV4 option(opt);
initStyleOption(&option, index);
painter->save();
const QStyle *style = option.widget ? option.widget->style() : QApplication::style();
const QWidget* widget = option.widget;
if( hover )
{
option.backgroundBrush = backBrush;
}
painter->save();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, widget);
painter->restore();
switch( index.column() )
{
case 0: // we want default behavior
style->drawControl(QStyle::CE_ItemViewItem, &option, painter, widget);
break;
case 1:
// some custom drawText
break;
case 2:
// draw a QStyleOptionProgressBar
break;
}
painter->restore();
}
その結果、個々のセルは、行全体ではなく、マウスがその上にあるときにのみ、mousedoverの背景を受け取ります。説明するのは難しいので、ここにスクリーンショットがあります: 上記のコードの結果http://www.binaryelysium.com/images/loader_bg.jpeg
その写真では、マウスが一番左のセルの上にあるため、背景が強調表示されています。しかし、行全体に背景を描画したいと思います。
どうすればこれを達成できますか?
編集:もう少し考えてみると、QStyle :: State_MouseOver状態は、マウスが置かれている実際のセルに対してのみ渡され、paintメソッドが行内の他のセルに対して呼び出された場合、QStyle::State_MouseOverは渡されません。セットする。
したがって、QStyle :: State_MouseOver_ Row状態(回答:いいえ)があるかどうかが問題になります。それを実現するにはどうすればよいですか?