PDA

View Full Version : QListWidget Deselect



mbrusati
16th December 2008, 15:19
Can QListWidget be made to allow at most one selected item? In other words, I need to create a list that can have 0 or 1 item selected. Clicking on a selected item deselects that item resulting in the selected items count to be 0.

jpn
16th December 2008, 17:41
I'm afraid there is no property you could just turn on. Perhaps the easiest (or least code) would be to override mousePressEvent(), create a copy of the QMouseEvent object so that control modifier is turned on, and pass it to the base class implementation. Then it would probably work in SingleSelection mode the way you want.

mbrusati
16th December 2008, 19:35
Thanks JPN for your quick reply. I was able to achieve the desired effect by connecting to the QListWidget::itemClicked signal. Here's an example of the kind of SLOT function I wrote:


void MyClass::listItemClicked( QListWidgetItem *item )
{
if ( previousSelection != NULL ) {
if ( item->text().operator==( previousSelection->text() ) ) {
previousSelection = NULL;
molList->clearSelection();
molList->clearFocus();
return;
}
}

previousSelection = item;
}