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. QStyledItemDelegate with custom paint() background color from selection / item-type

QStyledItemDelegate with custom paint() background color from selection / item-type

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 3.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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hello,

    I have a StyledItemDelegate with a custom paint method

    void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const
    {
        QWidget* editor = m_my_editor_widegt;      
    
        QStyleOptionViewItem opt = option;
        QStyledItemDelegate::initStyleOption(&opt, index);
    
        setDummyEditorData(editor, index);
        editor->resize(opt.rect.size());
        editor->setPalette(opt.palette);
        QPixmap pixmap(opt.rect.size());
        editor->render(&pixmap);
        painter->drawPixmap(opt.rect,pixmap);
    }
    

    the editor is a QWidget with other stuff in there (label, checkbox, lineedit, ...)
    The rowcolor of my view is dependent of the item type and is provided by ItemModel::data(Qt::BackgroundColorRole).

    How do I tell the delegate to draw the widget with the right color?

    Gojir4G 1 Reply Last reply
    0
    • gde23G gde23

      Hello,

      I have a StyledItemDelegate with a custom paint method

      void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const
      {
          QWidget* editor = m_my_editor_widegt;      
      
          QStyleOptionViewItem opt = option;
          QStyledItemDelegate::initStyleOption(&opt, index);
      
          setDummyEditorData(editor, index);
          editor->resize(opt.rect.size());
          editor->setPalette(opt.palette);
          QPixmap pixmap(opt.rect.size());
          editor->render(&pixmap);
          painter->drawPixmap(opt.rect,pixmap);
      }
      

      the editor is a QWidget with other stuff in there (label, checkbox, lineedit, ...)
      The rowcolor of my view is dependent of the item type and is provided by ItemModel::data(Qt::BackgroundColorRole).

      How do I tell the delegate to draw the widget with the right color?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      Hi @gde23,

      I think it should be something like that (not tested):

      QColor background = index.data(Qt::BackgroundRole).toColor();
      opt.palette.setColor(QPalette::Active, QPalette::Window, background)
      
      1 Reply Last reply
      2
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by gde23
        #3

        @Gojir4 said in QStyledItemDelegate with custom paint() background color from selection / item-type:

        QColor background = index.data(Qt::BackgroundRole).toColor();
        opt.palette.setColor(QPalette::Active, QPalette::Window, background)

        I've tried that, but that results in the widget looking totally colorful (each pixel random) like uninitialized memory.

        EDIT: Now its working, had to paint it with the background first since the background color of the items has alpha

        Gojir4G 1 Reply Last reply
        0
        • gde23G gde23

          @Gojir4 said in QStyledItemDelegate with custom paint() background color from selection / item-type:

          QColor background = index.data(Qt::BackgroundRole).toColor();
          opt.palette.setColor(QPalette::Active, QPalette::Window, background)

          I've tried that, but that results in the widget looking totally colorful (each pixel random) like uninitialized memory.

          EDIT: Now its working, had to paint it with the background first since the background color of the items has alpha

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @gde23 Glad it works !

          EDIT: Now its working, had to paint it with the background first since the background color of the items has alpha

          Sorry but I don't understand what do you mean ?

          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