QTableView show better seleced/focused Row



  • I have a QTableView and I must enable SingleSelection (Cell) for editing. So only the current cell gets a special background for selection.
    In this case I cannot really good see what row is/was selected, especial when I leave the tableview or I have a simple ActionButton delegate in a column.

    Is it possible to get a selection coloring like MS-Office/Access-Tables:

    The whole row gets a special background and the current selected cell an other background


  • Moderators

    @Andy314

    1. sublcass QStyledItemDelegate
    2. reimplement it's initStyleOption() method
    3. call the base class implementation first
    4. check the options state for selection and focus
    void MyItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
    {
        QStyledItemDelegate::initStyleOption(option, index);
    
        if( option->state & QStyle::State_Selected )
             option->backgroundBrush = QBrush( Qt::red );
    
        if( option->state & QStyle::State_HasFocus )
             option->backgroundBrush = QBrush( Qt::yellow );
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.