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 paint() and rowColor from Qt::BackgroundColorRole
Qt 6.11 is out! See what's new in the release blog

QStyledItemDelegate with paint() and rowColor from Qt::BackgroundColorRole

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.2k 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 QStyledItemDelegate with the following implementation of paint()

    void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
        const QModelIndex &index) const
    {
        .... here the editor is generated and so on
        ....
        ....
    
        // now the painting part begins
        QStyleOptionViewItem option_widget = option;
        QStyledItemDelegate::initStyleOption(&option_widget, index);
    
        // where to set the color???
        QColor background = index.model()->data(index, Qt::BackgroundColorRole).value<QColor>();
    
        // this is needed so the selection is painted correctly, otherwise the row-selection is not visible
        QStyleOptionViewItem option_widget_backgrund = option;
        QStyledItemDelegate::paint(painter, option_widget_backgrund, QModelIndex());
    
        // start drawing ------------------------------------------------------------
        painter->save();
        editor->setPalette(option_widget.palette);
        editor->resize(option_widget.rect.size());
        painter->translate(option_widget.rect.topLeft());
        editor->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
        painter->restore();
    }
    

    What I am doing here is first call QStyledItemDelegate::paint(painter, option_widget_backgrund, QModelIndex()); with an invalid index to paint the selection background.
    This works but is it really the correct way to paint the selection coloring??

    Now besides the selection coloring I want to colour the background of some rows as specified by Qt::BackgroundColorRole in the model.
    Where do I set this color?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QModelIndex has a data() function: https://doc.qt.io/qt-5/qmodelindex.html#data

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

        @Christian-Ehrlicher: but what do I do with the color data? My problem is that I do not know where to set it.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Setting data in the model can be done with QAbstractItemModel::setData()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • gde23G Offline
            gde23G Offline
            gde23
            wrote on last edited by
            #5

            Maybe I have not explained clearly what my problem is.

            I already have all the data in the model and I also have implemented the setData() and data() functions and so on.
            However now I want to have an QStyledItemDelegate in my View that has a custom paint() function.

            From the model I get the background color for each item with data(index, Qt::BackgroundColorRole)
            When I do not use delegates it also paints the color correctly.
            I just do not know how to use the color for the background in my custom paint() function.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              You don't really have a custom paint. You are just delegating the paint to a widget (editor->render()). You need to set up said widget to support changing its background

              "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

              1 Reply Last reply
              2

              • Login

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