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. [SOLVED] Question about Qt Table Widget Item appearence when selected
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.0k 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.
  • D Offline
    D Offline
    darkp03
    wrote on last edited by darkp03
    #1

    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!

    D 1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      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.

      D 1 Reply Last reply
      0
      • M mchinand

        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.

        D Offline
        D Offline
        darkp03
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D darkp03

          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!

          D Offline
          D Offline
          darkp03
          wrote on last edited by
          #4

          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

          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