Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Cannot draw checkbox in QStyledItemDelegate
QtWS25 Last Chance

[Solved] Cannot draw checkbox in QStyledItemDelegate

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cmannett85
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cmannett85
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cmannett85
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          Thank you for sharing your experience.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved