Results 1 to 3 of 3

Thread: Change font color of QListView row

  1. #1
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Change font color of QListView row

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change font color of QListView row

    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!

  3. The following user says thank you to Lykurg for this useful post:

    Phlucious (9th March 2012)

  4. #3
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change font color of QListView row

    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.

Similar Threads

  1. Replies: 3
    Last Post: 22nd January 2010, 17:46
  2. Replies: 6
    Last Post: 5th August 2009, 11:40
  3. Change Font of QListWidget to Monospace Font
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 19:23
  4. How do I change the font color in QTextBrowser?
    By probine in forum Qt Programming
    Replies: 2
    Last Post: 29th November 2006, 13:08
  5. Change Font Color of Label and CheckBox
    By mahe2310 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 15th June 2006, 11:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.