[Solved]QSpinBox not drawn correctly



  • I'm currently implementing a custom delegate, in part of which I need a QSpinBox to be drawn in the paint(..) method.

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

    //  Paint check box.
    QStyleOptionSpinBox spOpt;
    spOpt.palette     = option.palette;
    spOpt.rect        = option.rect;
    spOpt.state       = option.state;
    spOpt.frame       = true;
    spOpt.stepEnabled = QAbstractSpinBox::StepUpEnabled |
                       QAbstractSpinBox::StepDownEnabled;
    
    style->drawComplexControl( QStyle::CC_SpinBox, &spOpt, painter );
    
    painter->restore();
    

    }@

    Unfortunately it appears as:

    !http://i.stack.imgur.com/jMzaz.jpg(Error)!

    As you can see the step buttons are drawn massive and only the down arrow appears. Interestingly the width of the buttons mirrors that of the first table column, despite option.rect being the size of the cell (which is correct, which is presumably why the frame is drawn correctly).

    Any ideas what information I'm not giving QStyle?



  • Some styles are written with the assumption that they get painted to the widget starting at (0,0) since that is the normal way widgets will paint them, but item views will assign an offset to the style rect.

    Perhaps you could try something like the following to trick the style into thinking it is drawing at the origin:
    @
    painter->save();
    painter->translate(option.rect.x(), option.rect.y());
    // Paint check box.
    QStyleOptionSpinBox spOpt;
    spOpt.palette = option.palette;
    spOpt.rect = QRect(0, 0, option.rect.width(), option.rect.height());
    spOpt.state = option.state;
    spOpt.frame = true;
    spOpt.stepEnabled = QAbstractSpinBox::StepUpEnabled |
    QAbstractSpinBox::StepDownEnabled;

    style->drawComplexControl( QStyle::CC_SpinBox, &spOpt, painter );
    
    painter->restore();
    

    @



  • I have to say I didn't expect that to work as the frame and field subcomponents were in the right place and the right size - but I was thankfully proven wrong! Thank you.


Log in to reply
 

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