Results 1 to 8 of 8

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

  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)

  10. #7
    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?

    I made some progress by means of subclassing QSqlTableModel and extending data() function.

    Qt Code:
    1. QVariant MySqlTableModel::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant value = QSqlTableModel::data(index, role);
    4. if (role == Qt::TextColorRole && index.column() == 1)
    5. {
    6. // double d = value.toDouble();
    7. // if(d > 0)
    8. return QVariant::fromValue(QColor(Qt::blue));
    9. }
    10. return value;
    To copy to clipboard, switch view to plain text mode 

    After that, I tried to compare value with something else, which will led to paint or not the text. But it doesn't work (commented lines). May I ask some orientation on this issue?

    Also I am wondering about the benefits of the alternative approaches.

    Thanks for the valuable answers.


    Added after 29 minutes:


    It seems that the value could be retrieved with

    Qt Code:
    1. double d = this->record(index.row()).value(index.column()).toDouble();
    To copy to clipboard, switch view to plain text mode 
    Last edited by vrltwe; 25th July 2013 at 15:58.

  11. #8
    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 have the value that the model would normally return for the ForegoundRole, a null QVariant, not the value it would normally return for DisplayRole or EditRole. Comparing that to zero is not going to be useful. You can get the value the way you have, or using data() on the same index but Qt::EditRole inside the if bolck

  12. 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.