PDA

View Full Version : Selection in QTableWidget



Nippler
29th April 2008, 13:40
I like to color all selected cells in a QTablewidget. But this should be done if the user releases the mouse button. At the moment I have the following code in the table's itemSelectionChanged signal:


QList<QTableWidgetItem *> selections = table->selectedItems();

for(int i=0;i<selections.size();i++) {
int row=selections.at(i)->row();
int col=selections.at(i)->column();

QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setBackgroundColor(QColor("#0000FF"));
table->setItem(row, col, newItem);
}

The code is working fine so far, but if e.g. the user moves over the rows 0-3 but actually he only wants to select row 0-2 and he moves back to row 2 and releases the mouse button, row 3 is already colored because he moved already over it. I don't want that, in this case only row 0-2 should be colored.

How can I do that?

Thanks for answers!

jpn
29th April 2008, 14:23
How about storing the selection as a member variable? Once the selection changes, you clear the previously stored selection and colorize & store the new selection.

Nippler
30th April 2008, 09:08
Yes, that would work if I only want to color the actual selection. But I like to color every selection I make, so don't delete older colored fields.

My problem is that I don't want to color the fields already if I hover over the cells, I want to color the actual selection if I release the mouse button. So maybe something with mouse event...?!

Nippler
30th April 2008, 09:32
Problem solved:

I installed an event filter on the viewport of tablewidget and put the code I hade in itemSelectionChanged into the eventfilter method:

bool DialogClass::eventFilter(QObject* target, QEvent* event)
{

if (target==table->viewport()){


if(event->type() == QEvent::MouseButtonRelease){



//Code from first thread (look above)


}

}

return false;
}