Results 1 to 8 of 8

Thread: How to change the cell text color in a QTableView?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2013
    Posts
    22
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How to change the cell text color in a QTableView?

    I've tried this (without success):

    Qt Code:
    1. ui->tableView->model()->setData(ui->tableView->model()->index(5, 5, QModelIndex()), QVariant(QColor(Qt::red)), Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to change the cell text color in a QTableView?

    Qt::ForegroundRole
    Setting it with setData() is only useful if the model has somewhere to store the value. A QStandardItemModel does, for example, but a base QSqlTableModel does not.

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

    vrltwe (25th July 2013)

  4. #3
    Join Date
    Jul 2013
    Posts
    22
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change the cell text color in a QTableView?

    Hi ChrisW67,

    Thank you for your answer.

    But I still have no idea how to proceed in order to set the color of some cells. Could you please provide further information.

    Thanks in advance.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to change the cell text color in a QTableView?

    You are using a QTableView. Colours in that view are driven by the data in the model (or alternate behaviour implemented in a QStyledItemDelegate). You need to arrange for the underlying model to return the desired text colour when queried for the Qt::ForegroundRole. Without more information in the model you are actually using there not much more we can say.

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

    vrltwe (25th July 2013)

  7. #5
    Join Date
    Jul 2013
    Posts
    22
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change the cell text color in a QTableView?

    A QSqlTableModel is beeing used.

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to change the cell text color in a QTableView?

    QSqlTableModel does not return a value for Qt::ForegroundRole.

    You need to create a sub-class of QSqlTableModel with an extended data() function that returns something for Qt::ForegroundRole.
    Alternatively, place a QIdentityProxyModel subclass between the QSqlTableModel and the view and put the extended data() function in the proxy.
    Alternatively, write a QStyledItemDelegate sub-class that paints the cells differently (hardest to get right).

    Which option you choose depends on how many views are watching the model, whether the colour difference is unique to one view, etc.

  9. The following user says thank you to ChrisW67 for this useful post:

    vrltwe (25th July 2013)

Similar Threads

  1. how to color a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 13
    Last Post: 9th September 2015, 10:08
  2. Changing the background color of a cell in a QTableView
    By scarleton in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2010, 13:23
  3. Replies: 3
    Last Post: 22nd January 2010, 16:46
  4. how to change text color in QTableView?
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2008, 10:03
  5. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 11:22

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.