PDA

View Full Version : Change font color of QListView row



Phlucious
26th January 2012, 20:41
I have a QListView displaying a QStringListModel with a bunch of file paths loaded by the user. These file paths each correspond to a custom QFile that I developed. Once the user loads new paths, I verify whether the file at each path is valid (correct extension, formatting, version, etc). When the validation algorithm detects an invalid file, it emits a signal to that effect.

I would like to recolor items in the QItemView as red and add a tooltip to indicate that the file is invalid. Can I do this without subclassing QItemView, or can this be accomplished with QStyledItemDelegate? I don't really understand how delegates fit into the Model/View architecture since I'm not trying to create an editor.

I'm guessing that this is accomplished using Style Sheets, but I can't figure out how to access a single row's style sheet. Any pointer in the right direction would be appreciated.

Lykurg
26th January 2012, 22:27
You don't need to create an editor to use delegates...
Model: holds the data
View: shows the data
Delegate: defines how the data should be shown

So, when you receive a signal with the information that a file in invalid, just change the model. The change of anything else will be handled automatically. But for that you have to provide a proper delegate. So, subclass QStyledItemDelegate and reimp the data and paint methods. For the data method return what the base implementation returns except for the tooltip role (only when the path is wrong). And in the paint function you call the base implementation, but when the path is wrong, set a red color to the painter first. This way the text will appear red!

So try to do the delegate and post your efforts when you struggle and we will work on at this point!

Phlucious
9th March 2012, 18:42
I don't want this thread to die unacknowledged. My users wound up wanting a directory-based approach instead of a file-based approach, so I never got around to attempting to implement this. Thank you very much for the very useful post, though, and once I have the time I will take a stab at delegates. :)