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. How to format negative number as red colour in QTableWidget?
Forum Update on Tuesday, May 27th 2025

How to format negative number as red colour in QTableWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.1k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Piyush
    wrote on last edited by
    #1

    How do we format the QTableWidget such that negative numbers appear as red in colour

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Something like:

       QObject::connect(tableWidget->model(), &QAbstractItemModel::dataChanged, [ = ](const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int>& roles)->void{
          if(!roles.isEmpty() && !roles.contains(Qt::DisplayRole))
            return;
          const QVariant redBrush = QVariant::fromValue(QBrush(Qt::red));
          for(int i = topLeft.row(); i <= bottomRight.row(); ++i) {
            for(int j = topLeft.column(); j <= bottomRight.column(); ++j) {
              const QModelIndex currIdx = tableWidget->model()->index(i, j, topLeft.parent());
              if(currIdx.data(Qt::DisplayRole).toDouble() < 0.0) {
                if(currIdx.data(Qt::ForegroundRole) != redBrush) {
                  tableWidget->model()->setData(currIdx, redBrush , Qt::ForegroundRole);
                }
              } else if(currIdx.data(Qt::ForegroundRole).isValid()) {
                tableWidget->model()->setData(currIdx, QVariant(), Qt::ForegroundRole);
              }
            }
          }
        });
      
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      P 1 Reply Last reply
      5
      • VRoninV VRonin

        Something like:

         QObject::connect(tableWidget->model(), &QAbstractItemModel::dataChanged, [ = ](const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int>& roles)->void{
            if(!roles.isEmpty() && !roles.contains(Qt::DisplayRole))
              return;
            const QVariant redBrush = QVariant::fromValue(QBrush(Qt::red));
            for(int i = topLeft.row(); i <= bottomRight.row(); ++i) {
              for(int j = topLeft.column(); j <= bottomRight.column(); ++j) {
                const QModelIndex currIdx = tableWidget->model()->index(i, j, topLeft.parent());
                if(currIdx.data(Qt::DisplayRole).toDouble() < 0.0) {
                  if(currIdx.data(Qt::ForegroundRole) != redBrush) {
                    tableWidget->model()->setData(currIdx, redBrush , Qt::ForegroundRole);
                  }
                } else if(currIdx.data(Qt::ForegroundRole).isValid()) {
                  tableWidget->model()->setData(currIdx, QVariant(), Qt::ForegroundRole);
                }
              }
            }
          });
        
        
        P Offline
        P Offline
        Piyush
        wrote on last edited by
        #3

        @VRonin Thanks.
        This needs to be done in the python code? and is it possible to implement the same functionality using Qt style sheet?

        VRoninV 1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          it needs to be done in your code.
          it sets up a slot ( the lambda) to react to dataChanged signal from the model ( when view saves new value)

          Its not possible via style sheet as far as i know as it cant read the cell values.

          1 Reply Last reply
          3
          • P Piyush

            @VRonin Thanks.
            This needs to be done in the python code? and is it possible to implement the same functionality using Qt style sheet?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            This needs to be done in the python code?

            the part from [ = ] onward is a slot. you just have to declare a private slot in your class with that body (the translation to python should be straightforward. you can remove the QVector argument and the first if as it's just optimisation) and connect the dataChanged signal of tableWidget.model() to it

            and is it possible to implement the same functionality using Qt style sheet?

            Not alone and it would be a lot of work

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2

            • Login

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