Important: Please read the Qt Code of Conduct -

pyqt5 qtreeview QAbstractItemModel set coloring of single column based on function?

  • I'm quite new to qtreeview (using python) and I'm searching for a way have the color (background and text color) of a colum (not the whole item) of a qtreeview.(using QAbstractItemModel) to be based on the return value of a given function. Whenever the value of the column changes, the coloring should be updated.

    Do I somehow have to connect to a changed-event and read things about delegates or proxymodels and I did not really understand it...

    Anyone got a hint? - Thanks a lot in advance

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    In the data method of your model, you can run whatever computation you need to determine the colour to return for the BackgroundRole.

    As for updating, that is done in the setData method when a value is changed, emit the dataChanged signal accordingly. This will trigger the views to update their content and thus read your model again.

  • Great, I had missed that....
    I simply could not believe that it is actually that easy...

    Thanks a lot!

Log in to reply