HIghtlighting all items in selected row in the QTreeView



  • Hello everyone!

    I have very strange problem with visualize all items in the selected row in the QTreeView...

    I created my own item delegate which derives from the QStyledItemDelegate and I also use custom style derived from the QWindowStyle.
    Everything works fine when I use QTreeView with only one column. But when number of columns is greater then 1 painting goes wrong...

    Here is example code where I initialize QTreeView"

    QTreeView* pTreeView= new QTreeView( this);
     pTreeView->setModel( pModel );
     pTreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
    
    MyCustomDelegate( pDelegate  = new MyCustomDelegate();
    pTreeView->setItemDelegate( pDelegate );
    

    and here is some code from my custom Style:

    pPainter->save();
    if( pStyleOption->state & QStyle::State_HasFocus ) // Draws selection marker
    {
     pPainter->fillRect( pStyleOption->rect, QColor(255, 165, 6) );
     pPainter->setPen( QPen( Qt::black, 1, Qt::DotLine ) );
     QRect oRect = pStyleOption->rect.adjusted( 0,0,-1,-1 );
     pPainter->drawRect( oRect );
    }
    else if( pStyleOption->state & QStyle::State_Selected ) // Draws dotted selection marker
    {
     pPainter->setPen( QPen( Qt::black, 1, Qt::DotLine ) );
     QRect oRect = pStyleOption->rect.adjusted( 0,0,-1,-1 );
     pPainter->drawRect( oRect );
    }
    else // Draws no selection marker
    {
     pPainter->fillRect( pStyleOption->rect, QColor(255, 255, 255) );
    }
    pPainter->drawText( pStyleOption->rect.adjusted(2,0,0,0), pStyleOption->text );
    pPainter->restore();
    

    In such configuration only the most left item (left column) in the selected row is painted...

    I also added QTreeView option:
    pTreeView->->setAllColumnsShowFocus( true );

    but this result only in painting "else if" statement.

    Do You have any suggestions what is wrong?

    With best regards
    poor_robert



  • Hmmm,

    I look at qtreeview.cpp and drawRow(...) method implementation and it seems that for painting focus on all selected items in the row, "special" case is used... And in this "case" custom delegates are not used...

    So possible solution is to subclass QTreeView and reimplement ::drawRow(...) method with my own paint definition.



  • Try using the testFlag function instead of comparing the state and the flag directly:

    if (pStyleOption->state.testFlag(QStyle::State_Selected))
            pPainter->fillRect(pStyleOption->rect, QBrush(QColor(Qt::blue)));
    

Log in to reply
 

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