[Solved] Cannot draw checkbox in QStyledItemDelegate



  • I have a QStyledItemDelegate derived object for a QTableView derived view. I further delegate the painting and editor creation depending on the model index data type. For bools I wanted to represent the state via a checkbox - but the check box never appears.

    Here is the base delegate paint function:

    @void Sy_QtPropertyDelegate::paint( QPainter* painter,
    const QStyleOptionViewItem& option,
    const QModelIndex& index ) const
    {
    painter->save();

    if ( index.column() == 0 ) {
        ...
    } else {
        QVariant var = index.data();
        bool modified = index.data( Sy_QtPropertyModel::ModifiedRole ).toBool();
    
        //  If the data type is one of our delegates, then push the work onto
        //  that.
        auto it = delegateMap_.find( var.type() );
        if ( it != delegateMap_.end() ) {
            ( *it )->paint( painter, option, index );
        } else if ( var.type() != QVariant::UserType ) {
            ...
        } else {
            ...
        }
    }
    
    painter->restore();
    

    }@

    And the bool sub delegate paint function:

    @void Sy_boolPD::paint( QPainter* painter,
    const QStyleOptionViewItem& option,
    const QModelIndex& index ) const
    {
    painter->save();

    bool checked  = index.data().toBool();
    bool modified = index.data( Sy_QtPropertyModel::ModifiedRole ).toBool();
    
    QStyle* style = Sy_application::style();
    if ( modified ) {
        QStyleOptionViewItemV4 bgOpt( option );
        bgOpt.backgroundBrush = QBrush( Sy_QtPropertyDelegate::ModifiedColour );
        style->drawControl( QStyle::CE_ItemViewItem, &bgOpt, painter );
    }
    
    QStyleOption butOpt( option );
    butOpt.state = QStyle::State_Enabled;
    butOpt.state |= checked ? QStyle::State_On : QStyle::State_Off;
    style->drawControl( QStyle::CE_CheckBox, &butOpt, painter );
    
    painter->restore();
    

    }@

    If I force modified to be true, the background is colour of the table is appropriately changed, and cout-ing butOpt's rect and state members show that they are correct - but no check box is shown!

    I've worked with Qt's MVC framework a lot, but I cannot see where I have gone wrong here.



  • Setting

    @style->drawControl( QStyle::CE_CheckBox, &butOpt, painter );@

    To draw any other ControlElement type still causes nothing to render, so it's not a problem specific to check boxes.



  • All I had to do was look in the source code...

    @case CE_CheckBox:
    if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(option)) {
    ...
    }@

    The QStyleOption I pass to the method is cast to the type specific for drawing the control, CE_CheckBox requires a QStyleOptionButton, and if the cast fails the drawing operation silently skips.



  • Thank you for sharing your experience.


Log in to reply
 

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