Results 1 to 7 of 7

Thread: How to change the background colour of the QTableWidget?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default How to change the background colour of the QTableWidget?

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to change the background colour of the QTableWidget?

    See QAbstractScrollArea::viewport() and use a proper palette.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change the background colour of the QTableWidget?

    Thank u Lykurg,
    I tried like this .....
    Qt Code:
    1. QPalette pal=palette();
    2. pal.setColor(QPalette::Background,Qt::white);
    3. temp->viewport()->setPalette(pal);
    To copy to clipboard, switch view to plain text mode 


    But didnt get any effect...Please tell what am missing here?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to change the background colour of the QTableWidget?

    Ehm, as I thought. You even don't need to use viewport. But you have to use the right color role!
    Qt Code:
    1. QPalette p = w.palette();
    2. p.setColor(QPalette::Base, Qt::red);
    3. w.setPalette(p);
    4. w.show();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change the background colour of the QTableWidget?

    Lykung thanks a lot...
    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...

    Qt Code:
    1. void MainWindow::FilterCurrentTab(void)
    2. {
    3. temp= dynamic_cast<QTableWidget*>(ui->tabWidget_2->currentWidget());
    4. if (temp == NULL)
    5. {
    6. std::cout<<"Error. No Table widget found in Tab\n";
    7. return;
    8. }
    9.  
    10.  
    11. QPalette p=temp->palette();
    12. p.setColor(QPalette::Background,Qt::white); <----when next search begins all item will be in white background
    13.  
    14. temp->setPalette(p);
    15.  
    16.  
    17.  
    18. QString SearchKey=ui->lineEdit_FilterKey->text();
    19. LTempTable =temp->findItems(SearchKey,Qt::MatchExactly);
    20.  
    21. Findcounter=0;
    22. LTempTable.at(0)->setBackground(Qt::red);
    23. temp->scrollToItem(rowPtr,QAbstractItemView::EnsureVisible);
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::on_pushButton_findPrev_clicked()
    2. {
    3. QPalette p=temp->palette();
    4. p.setColor(QPalette::Background,Qt::white);
    5. temp->setPalette(p);
    6. Findcounter=Findcounter-1;
    7. cout<<"THE FIND COUNT:"<<Findcounter<<endl;
    8. LTempTable.at(Findcounter)->setBackground(Qt::red);
    9. temp->scrollToItem(LTempTable.at(Findcounter),QAbstractItemView::EnsureVisible);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::on_pushButton_FindNext_clicked()
    2. { QPalette p=temp->palette();
    3. p.setColor(QPalette::Background,Qt::white);
    4. temp->setPalette(p);
    5. Findcounter++;
    6. cout<<"THE FIND COUNT:"<<Findcounter<<endl;
    7.  
    8. LTempTable.at(Findcounter)->setBackground(Qt::red);
    9.  
    10. temp->scrollToItem(LTempTable.at(Findcounter),QAbstractItemView::EnsureVisible);
    11. }
    To copy to clipboard, switch view to plain text mode 
    please help me to solve this problem..
    Thanks in advance...
    Last edited by aurora; 11th January 2012 at 04:45.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to change the background colour of the QTableWidget?

    Well the background of the table widget surly does not overwrite the backgrounds of your items. Before highlighting the next/previous item simply change the old item back to white.

  7. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change the background colour of the QTableWidget?

    Or when you're starting to search change color for QPalette::Highlight on the table widget to red and 'select' item using setCurrentIndex().
    When you're done with searching set the default palette back.
    This way only currently selected items will have their background changed and as soon as you'll change to different item the previous one will go back to its old background.

Similar Threads

  1. Problem in QT treewidget background colour
    By keyurparekh in forum Qt Programming
    Replies: 2
    Last Post: 8th February 2011, 23:46
  2. Replies: 2
    Last Post: 17th July 2010, 21:07
  3. Replies: 4
    Last Post: 17th July 2010, 04:47
  4. QTextDocument background colour
    By Ruud v A in forum Qt Programming
    Replies: 6
    Last Post: 18th May 2010, 14:30
  5. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.