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. StyleSheet with QStyledItemDelegate.
Forum Updated to NodeBB v4.3 + New Features

StyleSheet with QStyledItemDelegate.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 5.1k 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.
  • S Offline
    S Offline
    samdol
    wrote on last edited by VRonin
    #1

    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-worxR 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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
      0
      • S samdol

        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-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @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
        2
        • VRoninV VRonin

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

          Could you show us the delegate paint method?

          S Offline
          S Offline
          samdol
          wrote on last edited by
          #4

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

          • Login

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