StyleSheet with QStyledItemDelegate.



  • I made a ListView to show pixmap as an icon and applied stylesheet.
    The stylesheet works if I don't apply CustomItemDelegate which is the subclassing of QStyledItemDelegate. The stylesheet also works fine if I don't reimplement paint() in CustomItemDelegate.

    Is there any method which makes it possible to apply stylesheet while I use QStyledItemDelegate where paint() is reimplemented?

     list_view = new QListView();
     list_view->setStyleSheet(" QListView::item:deselected { border: 1px solid black; border-radius: 6px; color: black; } QListView::item:selected {background: white; border: 4px solid green;}");
        itemDelegate = new CustomItemDelegate(list_view);
        list_view->setItemDelegate( itemDelegate );
     list_view->setModel(myModel);
    


  • Yes, you just need to use QApplication::style() when painting on the delegate.

    Could you show us the delegate paint method?


  • Moderators

    @samdol said in StyleSheet with QStyledItemDelegate.:

    Is there any method which makes it possible to apply stylesheet while I use QStyledItemDelegate where paint() is reimplemented?

    only calling the base class implementation (or do the respective painting call on the style)

    void paint(QPainter *painter,   const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
     QStyleOptionViewItem opt = option;
     this->initStyleOption(&opt, index);
    
        const QWidget *widget = option.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    }
    

    Now you could remove the text and the icon from the styleoption before passing it to the drawControl() method. This ensures that the background, border, selection is properly drawn.



  • @VRonin
    Here is my paint() for my delegate.
    CustomType is the metaType I defined(for example, stars).
    It displays well except I could not see the effect of setStylesheet anymore.
    Instead of QApplication::style(), I tried the suggestion (which is commented out), but I don't see any change.

    void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    if (index.data(Qt::DecorationRole).canConvert<CustomType>() ) {
    CustomType myType = qvariant_cast<CustomType>(index.data(Qt::DecorationRole));
    if (option.state & QStyle::State_Selected)
    painter->fillRect(option.rect, option.palette.highlight());
    else
    painter->fillRect(option.rect, option.palette.background());
    myType.paint(painter, option.rect, option.palette, CustomType::ReadOnly);

        QRect rect=option.rect;
        QRect button_rect;
        button_rect.setRect(rect.x(),0,rect.width(), myType.text_height);
        button_rect.setY(rect.y()+ myType.pixmap_size.height());
        button_rect.setHeight( myType.text_height );
        QStyleOptionButton button;
        button.rect = button_rect;
        button.text = myType.dirfn;
        button.state = state;
    
        QStyleOptionViewItem opt = option;
        this->initStyleOption(&opt, index);
    
          // const QWidget *widget = option.widget;
          // QStyle *style = widget ? widget->style() : QApplication::style();
          // style->drawControl(QStyle::CE_CheckBox, &button, painter);
    
        QApplication::style()->drawControl(QStyle::CE_CheckBox, &button, painter);
    
    } else {
        QStyledItemDelegate::paint(painter, option, index);
    }
    

    }


Log in to reply
 

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