Results 1 to 3 of 3

Thread: [QT4] Models and display roles - setting background color?

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default [QT4] Models and display roles - setting background color?

    Should I be looking at roles under the model's data() function to be able to set a background color for a given index? Or should I be looking elsewhere for such?

    Basically, I have a 'status' column, where I will have both text and a background color reflecting the status (red and green). Any ideas? Nothing I saw jumped out at me in the docs, unfortunately.
    Life without passion is death in disguise

  2. #2
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: [QT4] Models and display roles - setting background color?

    The QAbstractItemModel::data() method accepts an ItemDataRole enum value. You would need to override this and key off of Qt::BackgroundColorRole in order return the background color that you want. I think it is kind of strange for the view to ask the model for the color, but that's neither here nor there.

    Alternatively, you could implement your own item delegate that does custom painting and set it on the QAbstractItemView using the setItemDelegate method. This seems like a lot of work if you're just changing the background color of a column or row.

    You could also use the QTableWidget, which acts more like the Qt3 table and insert QTableWidgetItem's (you can set the background color on these directly).

    Hope this helps.

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

    KShots (10th March 2006)

  4. #3
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: [QT4] Models and display roles - setting background color?

    Thanks. I see what they did in the docs - I didn't realize they divided the tables like that. I looked at the top table of the screen and didn't see the lower tables were dealing with the same data. Thanks for the tip, that got it working great .
    Life without passion is death in disguise

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.