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();
}
결과적으로 각 개별 세포는 마우스가 전체 행이 아닌 마우스가 끝날 때만 마우스 오버 배경을 수신합니다. 설명하기가 어렵 기 때문에 여기 스크린 샷이 있습니다.위의 코드의 결과 http://www.binaryelysium.com/images/loader_bg.jpeg
그 그림에서 마우스는 왼쪽 셀 위에 있었으므로 강조 표시된 배경입니다. 그러나 나는 배경이 전체 행에 그려지기를 원합니다.
이것을 어떻게 달성 할 수 있습니까?
편집 : 좀 더 생각하면 Qstyle :: State_mouseover 상태가 마우스가 끝나는 실제 셀에 대해서만 전달되고 있으며, 페인트 방법이 행 qstyle :: state_mouseover가 아닌 다른 셀에 대한 페인트 방법이 호출 될 때 세트.
그래서 질문은 qstyle :: state_mouseover_가 있습니다.열 State (답 : 아니오), 어떻게 그것을 달성하려면 어떻게해야합니까?
해결책
마우스가 줄 이상일 때 셀을 업데이트하려면보기를 말해야하므로 모델에서이를 추적하는 것이 좋습니다. 그런 다음 페인트 이벤트에서 사용자 정의 데이터 역할을 사용하여 모델 인덱스에서 해당 데이터를 요청할 수 있습니다.
다른 팁
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;
}