PDA

View Full Version : How to change the background colour of the QTableWidget?



aurora
10th January 2012, 10:19
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?

Lykurg
10th January 2012, 11:19
See QAbstractScrollArea::viewport() and use a proper palette.

aurora
10th January 2012, 13:11
Thank u Lykurg,
I tried like this .....


QPalette pal=palette();
pal.setColor(QPalette::Background,Qt::white);
temp->viewport()->setPalette(pal);



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

Lykurg
10th January 2012, 13:33
Ehm, as I thought. You even don't need to use viewport. But you have to use the right color role!

QTableWidget w;
QPalette p = w.palette();
p.setColor(QPalette::Base, Qt::red);
w.setPalette(p);
w.show();

aurora
11th January 2012, 04:31
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...



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::EnsureVisib le);
}



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),QAbstractI temView::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),QAbstractI temView::EnsureVisible);
}




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

Lykurg
11th January 2012, 06:51
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.

Spitfire
11th January 2012, 10:11
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.