Results 1 to 2 of 2

Thread: Change focus/selection color for single cell in QTableView

  1. #1
    Join Date
    Jun 2014
    Posts
    30
    Thanks
    4
    Qt products
    Qt5

    Default Change focus/selection color for single cell in QTableView

    I have a table that renders a cell background green or red depending on its value. Here's a picture:

    Capture.png

    When I select/focus the table row, the green color is lost:

    Capture2.png

    I would like to keep a greenish color only for the single cell on focus/selection. Perhaps I would make it a slightly darker green color. How would I go about doing this?

    Here is my code that renders the cell green or red:

    Qt Code:
    1. QVariant ExternalDataSourcesTableModel::data(const QModelIndex & index, int32_t role) const
    2. {
    3. ...
    4.  
    5. if (role == Qt::BackgroundRole)
    6. {
    7. if (index.column() == static_cast<int>(Column::STATUS)) {
    8. if (externalDataSource.getStatus(reason) == "OK") {
    9. return QColor(Qt::green);
    10. }
    11. else {
    12. return QColor(Qt::red);
    13. }
    14. }
    15. }
    16.  
    17. ...
    18. }
    To copy to clipboard, switch view to plain text mode 

  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: Change focus/selection color for single cell in QTableView

    You create a QStyledItemDelegate subclass that implements paint() the way you want and then attach it to the column of the view with QAbstractItemView::setItemDelegateForColumn()

Similar Threads

  1. Replies: 3
    Last Post: 29th July 2014, 18:18
  2. Replies: 7
    Last Post: 25th July 2013, 21:47
  3. Different text styles in a single cell of QTableView?
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2009, 06:49
  4. cell selection in QTableView
    By user in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 01:01
  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.