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. How to change the background colour of the QTableWidget?
Qt 6.11 is out! See what's new in the release blog

How to change the background colour of the QTableWidget?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.9k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    aurora
    wrote on last edited by
    #1

    Is it possible to change the QTableWidgets background colour at once?
    Till now i was taking each QTableWidgetItem and changing its colour...
    But thats taking time and sometimes crashing program,
    So is there any other method to change the background colour of the QTableWidget?

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

      Change the viewport()'s palette? Stylesheets?

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maxim.prishchepa
        wrote on last edited by
        #3

        You can try change propertyes "base" in palette for parent widget yours QTableWidgets

        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aurora
          wrote on last edited by
          #4

          But my problem not yet solved....
          I tried to change its color as u told, it changes background color of all the items except the item which i changed before...

          Actually in my program i'm using a table widget and performing seraching operation on it....
          Whenever a item found with given search key, i'm changing its background colour to red....i'm providing "NEXT" and "PREv" button, which changes background of one after the other found item...

          But problem is that---1.At the begin i'll highlight the first found item, by changing its background color to red, when i click on "next", only that next item must highlight but in my case previously item not changing back its color to default white.....
          2. If i change the search key also, previously highlighted items(which is red background) not changing to default white color.....

          my code is as follows...
          @
          void MainWindow::FilterCurrentTab(void)
          {
          temp= dynamic_cast<QTableWidget*>(ui->tabWidget_2->currentWidget());
          if (temp == NULL)
          {
          std::cout<<"Error. No Table widget found in Tab\n";
          return;
          }

          QTableWidgetItem *rowPtr;
          QPalette p=temp->palette();
          p.setColor(QPalette::Background,Qt::white); <----when next search begins all item will be in white background
           
          temp->setPalette(p);
           
           
           
          QString SearchKey=ui->lineEdit_FilterKey->text();
          LTempTable =temp->findItems(SearchKey,Qt::MatchExactly);
           
          Findcounter=0;
          LTempTable.at(0)->setBackground(Qt::red);
          temp->scrollToItem(rowPtr,QAbstractItemView::EnsureVisible);
          }
          

          @

          @ void MainWindow::on_pushButton_findPrev_clicked()
          {
          QPalette p=temp->palette();
          p.setColor(QPalette::Background,Qt::white);
          temp->setPalette(p);
          Findcounter=Findcounter-1;
          cout<<"THE FIND COUNT:"<<Findcounter<<endl;
          LTempTable.at(Findcounter)->setBackground(Qt::red);
          temp->scrollToItem(LTempTable.at(Findcounter),QAbstractItemView::EnsureVisible);
          }
          @

          @
          void MainWindow::on_pushButton_FindNext_clicked()
          { QPalette p=temp->palette();
          p.setColor(QPalette::Background,Qt::white);
          temp->setPalette(p);
          Findcounter++;
          cout<<"THE FIND COUNT:"<<Findcounter<<endl;

          LTempTable.at(Findcounter)->setBackground(Qt::red);
           
          temp->scrollToItem(LTempTable.at(Findcounter),QAbstractItemView::EnsureVisible);
          }
          

          @

          please help me to solve this problem..
          Thanks in advance...

          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