どのようにしてQStyledItemDelegateに行全体の背景を描くのですか?
-
19-09-2019 - |
質問
私は上のカスタム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_の行の状態(答え:いいえ)があるので、どのように私はそれを達成する行くのです。
は?解決
あなたはマウスが列の上にあるときにその細胞を更新するビューを伝えることする必要があるので、私はあなたのモデルであることを追跡することをお勧め。その後、ペイントイベントでは、カスタムデータの役割を使用してモデルインデックスからそのデータを求めることができます。
他のヒント
void TrackDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem viewOption(option);
if (viewOption.state & QStyle::State_HasFocus)
viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
QImage image(m_RowBackGroundImagePath);
QPixmap pixmap(m_RowBackGroundImagePath);
qDebug()<<"forward"<<pixmap.width()<<pixmap.height();
pixmap.scaled(option.rect.width(),option.rect.height());
qDebug()<<"back"<<pixmap.width()<<pixmap.height();
qDebug()<<option.rect.width()<<option.rect.height();
QBrush brush(pixmap);
painter->save();
painter->fillRect(option.rect, brush/*QColor(238, 233, 233, 255)*/);
painter->restore();
viewOption.rect = QRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height());
//viewOption.palette.setColor(QPalette::Text, QColor(Qt::red));
//viewOption.palette.setBrush ( QPalette::ButtonText, brush1);
QItemDelegate::paint(painter, viewOption,index);
int progress = index.model()->data(index,Qt::DisplayRole).toInt();
QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = QRect(option.rect.x(), option.rect.y()+(SETHEIGHT - PROGRESSBARHEIGHT)/2, option.rect.width(), /*option.rect.height()*/PROGRESSBARHEIGHT);
//qDebug()<<progressBarOption.rect.x()<<progressBarOption.rect.y()<<progressBarOption.rect.height()<<progressBarOption.rect.width();
//qDebug()<<option.rect.x()<<option.rect.y()<<option.rect.height()<<option.rect.width();
progressBarOption.state |= QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.fontMetrics = QApplication::fontMetrics();
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
progressBarOption.progress = progress < 0 ? 0 : progress;
progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
break;
}