Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cant change row color background programatically



  • 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.



  • @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?



  • @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.



  • @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?



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



  • @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.



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


Log in to reply