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. Accessing object-specific stylesheet in custom delegate
QtWS25 Last Chance

Accessing object-specific stylesheet in custom delegate

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.6k 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.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by
    #1

    How do I access an object's custom stylesheet from within a custom delegate that's drawing for that object? My specific situation is:

    • I have a QListView. I customized the display of its items by defining a custom stylesheet in Designer. Among other things, I set QListView::item{padding: 5px};

    • I created a custom delegate in code (derived from QStyledItemDelegate) and passed it to my QListView object.

    Now within my delegate's paint() I don't know how to access the stylesheet I defined in Designer. Specifically I'd like to get the content rect that takes into account the item padding I defined - although I'd also like to know generally how to access everything defined in the stylesheet.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbnoimi
      wrote on last edited by
      #2

      Just input the name of the object as id in stylesheet ex.

      @#MyList {
      background-color: #f0f0f0;
      }

      #MyList::item:selected {
      border: 1px solid #6a6ea9;
      border-radius: 4px;
      background-color: white;
      color: black;
      }@

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #3

        That wasn't my question. The problem isn't that the stylesheet isn't associated with my object - if I just draw using the default delegate then it correctly picks up the style - the problem is that I'm unable to access the style from within a custom delegate.

        For example if I do this:

        @void MyCustomDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
        {
        QStyledItemDelegate::paint( painter, option, index );
        }@

        Then the stylesheet is respected. However when I try to manually draw things, ie:

        @QStyleOptionViewItemV4 opt = option;
        initStyleOption(&opt, index);
        QStyle* pStyle = opt.widget ? opt.widget->style() : QApplication::style();
        painter->save();
        pStyle->drawItemText(painter, opt.rect, Qt::AlignRight | Qt::AlignVCenter,
        pStyle->standardPalette(), true, "customText");
        painter->restore();@

        then the 'customText' is aligned all the way to the right, without respect for the padding defined in the style.

        How do I access the stylesheet from my delegate?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          There is no standard way to do such things.
          If you know, the style sheet is assigned to the widget, you can use pWidget->styleSheet(). If the style sheet is assigned to a higher level widget of the application, you have to scan the hierarchy.

          But there is no way I know, to get that part of a style sheet that is needed for your specific element. The style sheet is once parsed by the style sheet style and then internally handled in a binary way. all that is done in private classes of QtGui.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          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