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. QTableWidget: Set specific row's color
QtWS25 Last Chance

QTableWidget: Set specific row's color

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidgetqtableviewdelegate
10 Posts 3 Posters 26.5k 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
    sunil.nair
    wrote on last edited by
    #1

    Hi,

    I am using QTableWidget since it offers many features with respect to custom cell widgets animation etc. I want to set the color of a specific row at runtime. How do I do this?

    Is creating a delegate the only way to do this? How can it be done with delegates?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @sunil-nair
      It can done with QTableWidgetItem as follows:

      • setBackground

      • Or stylesheet

      QTableWidget::item {
          background-color: red;
      }
      

      157

      S 1 Reply Last reply
      2
      • p3c0P p3c0

        @sunil-nair
        It can done with QTableWidgetItem as follows:

        • setBackground

        • Or stylesheet

        QTableWidget::item {
            background-color: red;
        }
        
        S Offline
        S Offline
        sunil.nair
        wrote on last edited by
        #3

        @p3c0 ```
        ui->tablewidget-setitem(row, column, new QTableWidgetItem (string) ) ;
        ui->tablewidget-item(row, column)->setbackground(QBrush(QColor(250,0,0))):

        
        This code has no impact on the color of the qwidgetitem. 
        
        I also tried ```
        ui->tablewidget-item(row, column) ->setData(Qt::BackgroundRole, QColor (250,0,0)) 
        

        Even that doesn't work. I cannot use stylesheets since I want to change the color of a specific row at runtime.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @sunil-nair Check this example. To color the entire row you will need to iterate through all the cells and apply the same logic.

          157

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5
            void setRowBackground(const QBrush& brush, QAbstractItemModel* model, int row, const QModelIndex& parent = QModelIndex() ){
            if(!model || row<0 || row>=model->rowCount(parent))
            return;
            if(parent.isValid() && parent.model() != model){
            return;
            for(int i=0;i<model->columnCount(parent);++i)
            Q_ASSUME(model->setData(model->index(row,i,parent),brush,Qt::BackgroundRole));
            }
            

            now you can use it with:
            setRowBackground(QColor(250,0,0),ui->tablewidget->model(),row);

            "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

            S 1 Reply Last reply
            2
            • VRoninV VRonin
              void setRowBackground(const QBrush& brush, QAbstractItemModel* model, int row, const QModelIndex& parent = QModelIndex() ){
              if(!model || row<0 || row>=model->rowCount(parent))
              return;
              if(parent.isValid() && parent.model() != model){
              return;
              for(int i=0;i<model->columnCount(parent);++i)
              Q_ASSUME(model->setData(model->index(row,i,parent),brush,Qt::BackgroundRole));
              }
              

              now you can use it with:
              setRowBackground(QColor(250,0,0),ui->tablewidget->model(),row);

              S Offline
              S Offline
              sunil.nair
              wrote on last edited by
              #6

              @VRonin thanks for the reply. I tried your code. I am going into the model->setData block but I am not able to see any change in color. Do I need to do any more settings. I tried to also set a qstyleditemdelegate to my qtablewidget but the color doesn't change.

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

                Are you using a custom QAbstracItemModel or a custom QStyledItemDelegate?

                "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

                S 1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sunil.nair
                  wrote on last edited by
                  #8

                  I am using a qabstractitemmodel only. But it is not changing the row color.

                  VRoninV 1 Reply Last reply
                  0
                  • S sunil.nair

                    I am using a qabstractitemmodel only. But it is not changing the row color.

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

                    @sunil.nair said in QTableWidget: Set specific row's color:

                    I am using a qabstractitemmodel only

                    Your problem is in there then. could you post the code of your model?

                    "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
                    • VRoninV VRonin

                      Are you using a custom QAbstracItemModel or a custom QStyledItemDelegate?

                      S Offline
                      S Offline
                      sunil.nair
                      wrote on last edited by
                      #10

                      @VRonin hey it works. I created a separate qt project and it's working there. I will see if I am doing something wrong in my project.

                      Actually the row to which I am trying to set a background color also has some color set to it in table widget stylesheet. Also tablewidget items also have a background color. I will check there. Thanks again.

                      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