Important: Please read the Qt Code of Conduct -

How to paint the Person data on list and table view with delegate paint method

  • Hello guys!

    I have a trouble, I have Person data, model and delegate classes, Now I want to put all the information( including image icon of person) into the view cell, I must do it with paint() in delegate,
    so when I set the delegate to the view the cells should be filled with person data(including image it's important). How can I do it, can you give me some piece of code, here is what I started

    @ void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

      Person person = index.model()->data(index,Qt::DisplayRole).value<Person>();
      if(option.state & QStyle::State_Selected)


    it is very important for me and very thank you if you can help me

    [edit: corrected typo in guys SGaist]

  • Lifetime Qt Champion


    What are you currently getting ? What are you expecting ?

  • Now it paints only the icon and and overwrites the text
    I expect it to be Icon + the text and the size of icon must change when the cell size changes

  • You will need different rectangles for each piece of information you want to display if you want to avoid overlapping. Currently, you are using the same rect for all pieces of content.

  • and how can I customize to be in the next of each other and not together

  • There is different ways to do that. The QPainter::drawImage functions can do scaling for you, or you can use QImage::scaled (or QImage::scaledToHeight or scaledToWidth) to first scale and then paint.

Log in to reply