Results 1 to 5 of 5

Thread: TableView - Change background color if row have specific value

  1. #1
    Join Date
    Apr 2014
    Location
    PÅ‚ock, Poland
    Posts
    13
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default TableView - Change background color if row have specific value

    How I can change background color to red for row if row had timeToReply>15. I can't get timeToReply in rowDelegate. How I can do this ?

    Qt Code:
    1. TableView
    2. {
    3. model: someModel
    4. TableViewColumn{title:"Time";role: "timeToReply";width: 170}
    5. rowDelegate: Item {
    6. clip: true
    7. Text {
    8. anchors.verticalCenter: parent.verticalCenter
    9. color: {
    10. if(timeToReply>15) //problem
    11. {
    12. color="red"
    13. }
    14. else
    15. {
    16. color="black"
    17. }
    18. }
    19. text: styleData.value
    20. elide: Text.ElideMiddle
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TableView - Change background color if row have specific value

    You are using timeToReply as a role for TableViewColumn.

    Could that be a problem ?
    Try defining a property and using it instead

  3. #3
    Join Date
    Jun 2013
    Location
    Dresden - Germany
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: TableView - Change background color if row have specific value

    Hi,

    Where is the model from - QML or C++? Could you maybe show us the model? Maybe something is wrong there?
    Also you can shorten the color part a lot:

    Qt Code:
    1. color: timeToReply > 15 ? "red" : "black"
    To copy to clipboard, switch view to plain text mode 

    EDIT:
    I've tried it and yes I have the same problem, QtCreator gives me a ReferenceError, it seems it's not working like in a ListView. But it could be worked around by using ItemDelegate if you just want to change the text color, here you are reading the value from the Table (not the Role) and use it to modify the color. This way offers also a Table as always, without the need to rewrite all of the styling.

    Qt Code:
    1. TableView {
    2. id: table
    3. anchors.fill: parent
    4. model: testModel
    5. TableViewColumn {role: "timeToReply"; title: "Reply timings"; width: 100}
    6. itemDelegate: Item {
    7. Text {
    8. anchors.verticalCenter: parent.verticalCenter
    9. color: parseInt(styleData.value) > 15 ? "red" : styleData.textColor // maybe it's also working without parseInt().
    10. elide: styleData.elideMode
    11. text: styleData.value
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    AlphaX2
    Last edited by AlphaX2; 3rd July 2014 at 12:06.

  4. #4
    Join Date
    Apr 2014
    Location
    PÅ‚ock, Poland
    Posts
    13
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: TableView - Change background color if row have specific value

    Thanks AlphaX2, but it works half. I would do like that:
    aaad.png
    If column[TIME(MINUTES)]>15 color this row to red
    Last edited by updaterr; 3rd July 2014 at 13:35.

  5. #5
    Join Date
    Jun 2013
    Location
    Dresden - Germany
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: TableView - Change background color if row have specific value

    Hi again,

    it's also possible. Read the value not from styleData.value (content of the current cell and maybe a name not a number), but from the model directly. Because itemDelegate is delegate for every single cell this is working when you get the value from the model itself with the help of styleData.row.

    To be honest: it's a bit strange and counter intuitive, maybe it's a bug that you get a ReferenceError when you try to use the role Name in the rowDelegate (which is acting also a bit strange, as far as I've seen, while playing around with it.)

    Here is a working example:

    Qt Code:
    1. TableView {
    2. id: table
    3. anchors.fill: parent
    4. model: testModel
    5. TableViewColumn {role: "name"; title: "Name"; width: 75}
    6. TableViewColumn {role: "timeToReply"; title: "Reply timings"; width: 100}
    7. itemDelegate: Text {
    8. anchors.verticalCenter: parent.verticalCenter
    9. color: testModel.get(styleData.row).timeToReply > 15 ? "red" : "black"
    10. text: styleData.value
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    AlphaX2

  6. The following user says thank you to AlphaX2 for this useful post:

    updaterr (3rd July 2014)

Similar Threads

  1. Setting the background color of a header in TableView
    By sunilqt in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2013, 13:06
  2. Replies: 6
    Last Post: 27th September 2012, 23:44
  3. Replies: 1
    Last Post: 17th August 2010, 16:17
  4. how to change QTextEdit background color ?
    By mismael85 in forum Qt Programming
    Replies: 9
    Last Post: 26th June 2008, 22:05
  5. Change background color for a QPushButton?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 5th January 2007, 14:23

Tags for this Thread

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.