[Interest] Customize QTableView selection color

william.crocker at analog.com william.crocker at analog.com
Fri Feb 5 16:51:10 CET 2016


On 02/05/2016 10:34 AM, Murphy, Sean wrote:
> I’m still struggling with how to customize the selection color for items on a
> QTableView

I use a QStyledItemDelegate.
In the paint(painter,option,index) method I create my own QPalette based on
selection etc and then call the base class paint function.

     QStyleOptionViewItemV4 subop = option;
     QPalette pal = qApp->palette();
     if( selected ) {
        pal.setColor(QPalette::Highlight,Qt::darkGray);
        pal.setColor(QPalette::WindowText,Qt::white);
        pal.setColor(QPalette::HighlightedText,Qt::white);
        pal.setColor(QPalette::Text,Qt::white);
        subop.state |= QStyle::State_Selected;
        subop.palette = pal;
    }

    BaseClass::paint(painter,subop,index);	// Note: subop.

>(previously posted as
> http://lists.qt-project.org/pipermail/interest/2016-January/020760.html). I
> currently color the rows with custom colors based on the data being shown in
> each row in my model’s data(const QModelIndex &currIndex, int role) when role ==
> Qt::BackgroundRole.
>
> I’d like for that color to persist when the user selects a row; so either I want
> the selection to only paint the dotted line around the entire row without
> changing the background color to the OS’s default selection color, or I’d also
> be fine if there was no change at allwhen the row is selected. The only reason I
> allow rows to be selected at all is apparently that is required to allow the
> user to start a drag on a row when they are attempting to reorder the rows in
> the table. I can’t seem to figure out where this needs to happen.
>
> Correct me if I’m wrong on any of the following:
>
> 1.I can’t use a stylesheet because the colors are different on a row-by-row basis
>
> 2.I don’t think the selection background color is controlled anywhere in the model?
>
> 3.I really don’t have any need for the user to be able to select anything – the
> table is meant to be a read-only display of data - EXCEPT it appears that being
> able to select items is a requirement for the drag-n-drop system?
>
> Any pointers?
>
> Sean
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list