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. Cant change row color background programatically
Qt 6.11 is out! See what's new in the release blog

Cant change row color background programatically

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 713 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.
  • L Offline
    L Offline
    LCorona
    wrote on last edited by
    #1

    This is rare, maybe the time of compiling the object is peculiar, but the code cant change the background color of the rows in table widget.

       foreach(QModelIndex index,ui->channelsTable->selectionModel()->selectedIndexes())
       {
            if(selectedRow != index.row())
           {
                 indexes.append(index.row());
                 ui->channelsTable->item(indexes.last(),0)->setBackground(QBrush(Qt::blue));
                 ui->channelsTable->item(indexes.last(),1)->setBackground(QBrush(Qt::blue));
           }
         selectedRow = index.row();
    }
    

    The instruction

    ui->channelsTable->item(indexes.last(),0)->setBackground(QBrush(Qt::blue));
    

    not work if the number of row and column is not a number like this.

    ui->channelsTable->item(3,0)->setBackground(QBrush(Qt::blue));
    

    the indexes is QVector<int>indexes, the debug output print that the index numbers are correctly stored in the vector.

    JonBJ 1 Reply Last reply
    0
    • L LCorona

      This is rare, maybe the time of compiling the object is peculiar, but the code cant change the background color of the rows in table widget.

         foreach(QModelIndex index,ui->channelsTable->selectionModel()->selectedIndexes())
         {
              if(selectedRow != index.row())
             {
                   indexes.append(index.row());
                   ui->channelsTable->item(indexes.last(),0)->setBackground(QBrush(Qt::blue));
                   ui->channelsTable->item(indexes.last(),1)->setBackground(QBrush(Qt::blue));
             }
           selectedRow = index.row();
      }
      

      The instruction

      ui->channelsTable->item(indexes.last(),0)->setBackground(QBrush(Qt::blue));
      

      not work if the number of row and column is not a number like this.

      ui->channelsTable->item(3,0)->setBackground(QBrush(Qt::blue));
      

      the indexes is QVector<int>indexes, the debug output print that the index numbers are correctly stored in the vector.

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

      @LCorona said in Cant change row color background programatically:

      not work if the number of row and column is not a number like this.

      What does this mean? In fact, what is your whole question?

      L 1 Reply Last reply
      0
      • JonBJ JonB

        @LCorona said in Cant change row color background programatically:

        not work if the number of row and column is not a number like this.

        What does this mean? In fact, what is your whole question?

        L Offline
        L Offline
        LCorona
        wrote on last edited by
        #3

        @JonB
        The command

         foreach(QModelIndex index,ui->channelsTable->selectionModel()->selectedIndexes())
        

        Not work properly, maybe is a glitch, the reported selected rows in the table are all duplicated, if I have selected 3 rows, the list report 6, I dont know why, and I dont care, the remains instructios inside loop works for only register the selected rows ingnoring the duplicates.

        My question is very simply, cant change the background color of a row, the instruccion is ignored I dont know why. Only works if put the row and column directly with a number.

        JonBJ 1 Reply Last reply
        0
        • L LCorona

          @JonB
          The command

           foreach(QModelIndex index,ui->channelsTable->selectionModel()->selectedIndexes())
          

          Not work properly, maybe is a glitch, the reported selected rows in the table are all duplicated, if I have selected 3 rows, the list report 6, I dont know why, and I dont care, the remains instructios inside loop works for only register the selected rows ingnoring the duplicates.

          My question is very simply, cant change the background color of a row, the instruccion is ignored I dont know why. Only works if put the row and column directly with a number.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @LCorona said in Cant change row color background programatically:

          Not work properly

          It works fine.

          if I have selected 3 rows, the list report 6

          selectedIndexes() returns all the indexes selected, and that means every cell in every selected row. You seem to have 2 columns, so that's why.

          Only works if put the row and column directly with a number.

          So you are claiming ui->channelsTable->item(3,0)->setBackground() works but ui->channelsTable->item(indexes.last(),0)->setBackground(), when indexes.last() == 3? And you have put a qDebug() << indexes.last() on the line immediately above and verified it outputs 3?

          L 1 Reply Last reply
          1
          • JonBJ JonB

            @LCorona said in Cant change row color background programatically:

            Not work properly

            It works fine.

            if I have selected 3 rows, the list report 6

            selectedIndexes() returns all the indexes selected, and that means every cell in every selected row. You seem to have 2 columns, so that's why.

            Only works if put the row and column directly with a number.

            So you are claiming ui->channelsTable->item(3,0)->setBackground() works but ui->channelsTable->item(indexes.last(),0)->setBackground(), when indexes.last() == 3? And you have put a qDebug() << indexes.last() on the line immediately above and verified it outputs 3?

            L Offline
            L Offline
            LCorona
            wrote on last edited by
            #5

            @JonB
            Yes, absolutely correct. The command ui->channelsTable->item(X,X)->setBackground() not work programatucally with variables in the parameteres.

            JonBJ 1 Reply Last reply
            0
            • L LCorona

              @JonB
              Yes, absolutely correct. The command ui->channelsTable->item(X,X)->setBackground() not work programatucally with variables in the parameteres.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @LCorona
              No, that cannot be.

              I suggest you put a qDebug() << index.row() << index.column(); in as the first statement in the loop. You may find that the selected indexes are not coming out in an order you expect them to be. Not that I can see that will make any difference given your algorithm. However, in itself this has nothing to do with "it works with a literal number but not with a variable whose value is that number", as you are claiming, which would be a fundamental fault in the C++ compiler.

              L 1 Reply Last reply
              1
              • JonBJ JonB

                @LCorona
                No, that cannot be.

                I suggest you put a qDebug() << index.row() << index.column(); in as the first statement in the loop. You may find that the selected indexes are not coming out in an order you expect them to be. Not that I can see that will make any difference given your algorithm. However, in itself this has nothing to do with "it works with a literal number but not with a variable whose value is that number", as you are claiming, which would be a fundamental fault in the C++ compiler.

                L Offline
                L Offline
                LCorona
                wrote on last edited by LCorona
                #7

                @JonB
                Ok, I will do that, must be a problem with my variables.

                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