[SOLVED] Question about Qt Table Widget Item appearence when selected



  • Hi everyone!

    I have a question, but I dont know if its actually possible to do with Qt.
    I have a QTableWidget populated with QTableWidgetsItems on every cell.
    On my program, the user must be able to select certain amounts of cells, and, when he presses a button, the color of the cells should change.
    I was able to do this with no problem at all, the only thing thats a little disturbing, is that while the cell is selected, the color cant be seen, because the cell will appear as light blue Once that cell loses focus, you can see the color the user chose. I would like to know if it is possible to prevent a cell turning light blue when selected, so that the user can see the cells background color.
    Ive tried changing the items flags, but If I dont make it enabled, the appearence will look good (it wont be light blue) but It wont allow me to change the colors, because the user wont be able to select the cells, so this wont work.

    This is how I initialize my Table

    for(int i=0;i<4;i++)
    {
         for(int j=0;j<32;j++)
         {
              QTableWidgetItem *Item;
              Item=new QTableWidgetItem;
              Item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
              Item->setBackgroundColor(Qt::white);
              ui->tableWidget_E1->setItem(i,j,Item);
         }
    }
    
    

    And this is how I paint my selected cells

    QModelIndexList List=ui->tableWidget_E1->selectionModel()->SelectedIndexes();
    for(int i=0;i<List.size();i++)
    {
         QModelIndex Cell=List.at(i);
         ui->tableWidget_E1->item(Cell.row(),Cell.column())->setBackgroundColor(Color);
    }
    

    Where Color is a QColor initialized somewhere else in the program.

    I will appreciate any suggestion. Thanks in advance!



  • Use Qt Stylesheets. Something like the following:

    QTableView {
         selection-background-color: blue;
         selection-color: black;
    }
    

    The color can be specified in a number ways; see the color section of the QSS property types.



  • @mchinand

    Wow, thanks! To be hones, I didnt even know about the existence of Stylesheets. Seems quite starightforward, nevertheless I still havent achieved what I wanted to.
    Changing the selection-background-color allows me to choose what color it will turn when selected, but I actually what I want the table to do, is to show the cells color, or at least, the more "darker" version of that color, or something like that, not replacing it by a complete new color.
    For example on excel, when you select a specific cell, the program will show it to you by painting it of a slightly darker color of the one it already has. I would need something like this.
    I tried doing

    selection-background-color: QColor(100,0,0,125);

    Hoping the 125 will create a slight transparent color, but It didnt work.
    Any ideas?



  • Just wanted to say that after surfing the web for a long time, I Found the solution on this really old post
    http://www.qtforum.org/article/34125/disable-qtablewidget-selection-color.html
    it works like a charm!
    Regards


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.