Question

I have a QTableView which I am setting a custom QStyledItemDelegate on.

In addition to the custom item painting, I want to style the row's background color for the selection/hovered states. The look I am going for is something like this KGet screenshot: KGet's Row Background http://www.binaryelysium.com/images/kget_background.jpeg

Here is my code:

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();
}

The result is that each individual cell receives the mousedover background only when the mouse is over it, and not the entire row. It is hard to describe so here is a screenshot: The result of the above code http://www.binaryelysium.com/images/loader_bg.jpeg

In that picture the mouse was over the left most cell, hence the highlighted background.. but I want the background to be drawn over the entire row.

How can I achieve this?

Edit: With some more thought I've realized that the QStyle::State_MouseOver state is only being passed for actual cell which the mouse is over, and when the paint method is called for the other cells in the row QStyle::State_MouseOver is not set.

So the question becomes is there a QStyle::State_MouseOver_Row state (answer: no), so how do I go about achieving that?

Was it helpful?

Solution

You need to be telling the view to update its cells when the mouse is over a row, so I would suggest tracking that in your model. Then in the paint event, you can ask for that data from the model index using a custom data role.

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top