Important: Please read the Qt Code of Conduct -

[SOLVED] Custom Delegate to repaint() if data changes in model.

  • Hi,

    I have a customDelegate that subclasses from QStyledItemDelegate for my tableView. Foreach cell in the tableView I am storing an object of a class eg UserInformation. I am also displaying an image inside the tableView which is set based on a boolean variable. The pixmap/image is set as enabled/disabled in the paint() function of the delegate.

    Whenever the boolean values changes i need to display different image. (enabled/disabled)
    But when the values are changes it does not update the tabvleView. Only if i click on the tableView then the updated images are displayed. I tried to call ui->tableView->repaint() . but nothing changes. Only if i click or resize the window then the latest images gets displayed.

  • How did you implement the change of the value?
    Typically, you do it in data model setData where you emit a dataChanged signal, which will enforce the repaint.

    If you call talbeView->repaint, you repaint the background. tableView->viewport()->repaint would repaint the whole table, which is no god style by the way.

  • @Gerolf


    Yes tableView->viewport()->repaint() does the work. Actually I am setting the pixmap for the particular column inside the paint() function of the delegate (which may be the wrong way of doing).
    So in the paint() function it checks for a boolean value. If true then display enabled else display disabled image.

    Then inside my MainWindow based on some calculations i am setting this boolean value to true or false.

    What approach should i follow. Should i add the icon/pixmap to the item or should i display the same inside the paint() function.

  • It's important to emit the dataChanged() signal. Not only for the index whose boolean value changed, but also for the index that should be redrawn with another pixmap. You can either emit the signal twice, or use the range of indexes that it can contain. This should be sufficient to redraw your data.

  • Thanks for the answers, I have made the required changes including emit dataChanged(). This is solved.

    Thanks for your time :)

Log in to reply