PDA

View Full Version : QListWidget::currentItem() issue



high_flyer
13th April 2007, 21:39
Hi,

I connected the QListWidget::itemSelectionChanged() signal to a slot.
In the slot I extract the selected item string - and append it to another string.
The problem is, that the currentItem() always returns the previous selected item - when I select using the mouse (even if I select one item after the other in the list - so the focus always stays on the list).
However, if I change the selection with the keybaord (with the arrows) currentItem() returns the correct item (since I know that per definition, currentItem() is the item with keyboard focus.
But shouldn't the mouse selection also count in this case? (could this be a bug?)
And what can I do in order to have the correct current item when selecting with the mouse?

The reason I am using the itemSelectionChanged() signal, is that it is the only one that is emitted with out a parameter when a QListWidget is clicked.
And I don't want one with a parameter, since this slot reacts to several list selections, and works on several currentItem()'s.

Thanks in advance.

jpn
13th April 2007, 22:17
But shouldn't the mouse selection also count in this case? (could this be a bug?)

And what can I do in order to have the correct current item when selecting with the mouse?
Yeah, it seems a bit weird. I can reproduce the issue. But if you're specially interested about the current item, how about using something like

connect(listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), receiver, SLOT(doSomething()));
?

davit
15th April 2007, 08:23
Hi,

I think, if you want to work with selected item, you don't need to use currentItem() method, because in some cases current item and selected item can be different (for example ctrl-click on selected item if your listWidget has ExtendedSlection selection mode). Instead you can implement your own current_item() method like this:


QListWidget list = new QListWidget;
QListWidgetItem* current_item()
{
QList<QListWidgetItem*> l = list->selectedItems();
if ( ! l.empty()) {
return l.at(0);
}
}

In single selection mode this is excellent code to synchronizing current and selected items. In ExtendedSelection mode it will return always first selected item.



Sorry for mistake. And also you need to return NULL if there is no selection (at the end of function current_item())

Thanks,
--davit

high_flyer
16th April 2007, 10:23
Thank you both.
Jpn - I am aware I can do that - but I was curious if there is a "correct" way to do this that I might not be aware of (since I am not that experienced with data aware widgets in Qt4 - yet).
What you suggested just seems "dirty", but I will probably use it since I have no choice.
Do you guys think this counts as a bug? should I report it?

davit: I have just a simple lists, with single selction, in which I need to know which item is currently selected.
So for that your code is an overkill - thanks never the less!

jpn
16th April 2007, 11:04
Jpn - I am aware I can do that - but I was curious if there is a "correct" way to do this that I might not be aware of (since I am not that experienced with data aware widgets in Qt4 - yet).
What you suggested just seems "dirty", but I will probably use it since I have no choice.
The point is that still if selected items and the current item have a relation of some kind, one should not monitor the selection in case he is interested about the current item. And vice versa. They are related, but different concepts. ;)