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 color column
Qt 6.11 is out! See what's new in the release blog

Qtableview color column

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.6k 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.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by n-2204
    #1

    i need to set color to column using data() but this code is not working why ?

      QVariant tool:: data(const QModelIndex& index, int role) const 
     {
        QAbstractItemModel* table1 = ui.tableView->model();
       int col = table1->columnCount();
       switch (role) {
    
    case Qt::BackgroundRole:
    {
        switch (col)
        {
        case 1:
            return QColor(Qt::yellow);
        case 2:
            return QColor(Qt::yellow);
        }
    }
    default:
        break;
    }
    return QVariant();
     }
    

    Thanks in advance.

    VRoninV 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #7
      for (int row = 0; row < table1->rowCount(); ++row) {
          for (int column : {1,2,3,4,5,9,10}) 
              table1->setData(table1->index(row, column), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
      }
      QObject::connect(table1, &QAbstractItemModel::rowsInserted, table1, [table1](const QModelIndex& parent, int first, int last) {
          for (int row = first;row <= last;++row){
              for (int column : {1,2,3,4,5,9,10})
                  table1->setData(table1->index(row,column,parent),QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
          }
      });
      

      "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
      0
      • N n-2204

        i need to set color to column using data() but this code is not working why ?

          QVariant tool:: data(const QModelIndex& index, int role) const 
         {
            QAbstractItemModel* table1 = ui.tableView->model();
           int col = table1->columnCount();
           switch (role) {
        
        case Qt::BackgroundRole:
        {
            switch (col)
            {
            case 1:
                return QColor(Qt::yellow);
            case 2:
                return QColor(Qt::yellow);
            }
        }
        default:
            break;
        }
        return QVariant();
         }
        

        Thanks in advance.

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

        @n-2204 said in Qtableview color column:

        this code is not working why ?

        int col = table1->columnCount();

        "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

        N 1 Reply Last reply
        3
        • VRoninV VRonin

          @n-2204 said in Qtableview color column:

          this code is not working why ?

          int col = table1->columnCount();

          N Offline
          N Offline
          n-2204
          wrote on last edited by
          #3

          @VRonin this i am using to get column no. what to use instead of this ?

          VRoninV 1 Reply Last reply
          0
          • N n-2204

            @VRonin this i am using to get column no. what to use instead of this ?

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

            @n-2204 said in Qtableview color column:

            this i am using to get column no

            No, think, what that line does. If it helps check with a debugger what its value is

            "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

            N 1 Reply Last reply
            0
            • VRoninV VRonin

              @n-2204 said in Qtableview color column:

              this i am using to get column no

              No, think, what that line does. If it helps check with a debugger what its value is

              N Offline
              N Offline
              n-2204
              wrote on last edited by
              #5

              @VRonin ohk
              looks like even not going in this part of code as i haven't connect this

              1 Reply Last reply
              0
              • N Offline
                N Offline
                n-2204
                wrote on last edited by n-2204
                #6

                @VRonin using this code i am able color the column and row but when i am adding new row color is not updated to specified column

                   for (int row = 0; row < table1->rowCount(); ++row) {
                   for (int column = 0; column < table1->columnCount(); ++column) {
                  if (column == 1 || column == 2 || column == 3 || column == 4 ||
                      column == 5 || column == 9 || column == 10) {
                    const QModelIndex index1 = model->index(row, column);
                     table1->setData(index1, QColor(240, 248, 255), Qt::BackgroundRole);
                
                QObject::connect(table1, &QAbstractItemModel::rowsInserted, table1, [table1]
                (const QModelIndex& parent, int first, int last) {
                    for (int i = first;i <= last;++i)
                
                what to pass in this index()? so when add row color should be added on the mentioned column
                        table1->setData(index(),QColor(240, 248, 255), Qt::BackgroundRole);
                                });
                        }
                        }
                    }
                
                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #7
                  for (int row = 0; row < table1->rowCount(); ++row) {
                      for (int column : {1,2,3,4,5,9,10}) 
                          table1->setData(table1->index(row, column), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
                  }
                  QObject::connect(table1, &QAbstractItemModel::rowsInserted, table1, [table1](const QModelIndex& parent, int first, int last) {
                      for (int row = first;row <= last;++row){
                          for (int column : {1,2,3,4,5,9,10})
                              table1->setData(table1->index(row,column,parent),QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
                      }
                  });
                  

                  "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
                  0

                  • Login

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