Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved StyleSheet with QStyledItemDelegate.

    General and Desktop
    3
    4
    4251
    Loading More Posts
    • 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.
    • S
      samdol last edited by VRonin

      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);
      
      raven-worx 1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

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

        Could you show us the delegate paint method?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        S 1 Reply Last reply Reply Quote 0
        • raven-worx
          raven-worx Moderators @samdol last edited by

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 2
          • S
            samdol @VRonin last edited by

            @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);
            }
            

            }

            1 Reply Last reply Reply Quote 0
            • First post
              Last post