Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableview row color vanishes when focus lost
Forum Updated to NodeBB v4.3 + New Features

QTableview row color vanishes when focus lost

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 251 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    schotter
    wrote on last edited by
    #1

    Hi,
    I have a model (ClientsModel) and a proxymodel (ClientsProxyModel) for it., The proxy should take care of some coloring.
    It does so by querying another model and asking for the latest entry's timestamp.
    If the entry was done within the last hour, the row color is set to "green".

    QVariant ClientsProxyModel::data(const QModelIndex &index, int role) const
    {
      if (!index.isValid())
        return QVariant();
    //ForegroundRole or BackgroundRole
      if(role == Qt::ForegroundRole) {
        QModelIndex idxClient = sourceModel()->index(index.row(), int(ClientsModel::Column::ID));
        int clientId = sourceModel()->data(idxClient).toInt();
        QDateTime lastTS = anotherModel->getLastEntryTS(clientId);
        QDateTime now = QDateTime::currentDateTime();
        now = now.addSecs(-3600);
        if(lastTS > now) {
          return QBrush(QColor(55, 200, 40, 255));   //ff c9 2a
        } else {
          return {};
        }
      } else {
        return(sourceModel()->data(index, role));
      }
    return {};
    }
    

    It works, but unfortunately the moment the mouse cursor leaves the tableview widget colors are reset to defaults.
    Is there a different Qt::xyzRole I have to look into or do I have to change some (tableview) settings to fix this?

    Kind regards,
    schotter

    JonBJ 1 Reply Last reply
    0
    • S schotter

      Hi,
      I have a model (ClientsModel) and a proxymodel (ClientsProxyModel) for it., The proxy should take care of some coloring.
      It does so by querying another model and asking for the latest entry's timestamp.
      If the entry was done within the last hour, the row color is set to "green".

      QVariant ClientsProxyModel::data(const QModelIndex &index, int role) const
      {
        if (!index.isValid())
          return QVariant();
      //ForegroundRole or BackgroundRole
        if(role == Qt::ForegroundRole) {
          QModelIndex idxClient = sourceModel()->index(index.row(), int(ClientsModel::Column::ID));
          int clientId = sourceModel()->data(idxClient).toInt();
          QDateTime lastTS = anotherModel->getLastEntryTS(clientId);
          QDateTime now = QDateTime::currentDateTime();
          now = now.addSecs(-3600);
          if(lastTS > now) {
            return QBrush(QColor(55, 200, 40, 255));   //ff c9 2a
          } else {
            return {};
          }
        } else {
          return(sourceModel()->data(index, role));
        }
      return {};
      }
      

      It works, but unfortunately the moment the mouse cursor leaves the tableview widget colors are reset to defaults.
      Is there a different Qt::xyzRole I have to look into or do I have to change some (tableview) settings to fix this?

      Kind regards,
      schotter

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @schotter
      To me this code looks fine. You could try in a standalone program, to rule out any CSS or selection/focus stuff your code could have added.

      If color changes when "the mouse cursor leaves the tableview widget" that sounds odd. If it's to do with colors when focussed/unfocussed what platform/window manager are you running under?

      You could just try something with CSS only to see how that behaves?

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @schotter
        To me this code looks fine. You could try in a standalone program, to rule out any CSS or selection/focus stuff your code could have added.

        If color changes when "the mouse cursor leaves the tableview widget" that sounds odd. If it's to do with colors when focussed/unfocussed what platform/window manager are you running under?

        You could just try something with CSS only to see how that behaves?

        S Offline
        S Offline
        schotter
        wrote on last edited by
        #3

        @JonB thanks for your input :)

        I found the source of my problem. It's how I handle my data in the model.
        The data behind anotherModel->getLastEntryTS(clientId); changes whenever a client in the same tableview is selected. It basically only contains the selected client's data. So when the cursor is moved to a different row (another client), the old "green" row stays "green", because the view is not updated for that old row. But when the cursor moves back, the data for the "old" client isn't present anymore and therefore I can't read a correct timestamp from the model.
        Maybe my mistake helps someone else but I have my doubts^^

        1 Reply Last reply
        0
        • S schotter has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved