Results 1 to 5 of 5

Thread: QListWidget::currentItem() issue

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QListWidget::currentItem() issue

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidget::currentItem() issue

    Quote Originally Posted by high_flyer View Post
    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
    Qt Code:
    1. connect(listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), receiver, SLOT(doSomething()));
    To copy to clipboard, switch view to plain text mode 
    ?
    Last edited by jpn; 13th April 2007 at 22:32. Reason: spelling error
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    13
    Qt products
    Qt4

    Default

    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:

    Qt Code:
    1. QListWidgetItem* current_item()
    2. {
    3. QList<QListWidgetItem*> l = list->selectedItems();
    4. if ( ! l.empty()) {
    5. return l.at(0);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by wysota; 15th April 2007 at 09:02. Reason: Posts merged

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget::currentItem() issue

    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!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidget::currentItem() issue

    Quote Originally Posted by high_flyer View Post
    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.
    J-P Nurmi

Similar Threads

  1. qt3 to qt4 - uic issue
    By hvengel in forum Qt Programming
    Replies: 10
    Last Post: 4th March 2007, 02:59
  2. Database access issue
    By Gayathri in forum Newbie
    Replies: 3
    Last Post: 23rd November 2006, 07:41
  3. Dialog and code design issue
    By Gopala Krishna in forum Qt Programming
    Replies: 1
    Last Post: 24th September 2006, 17:54
  4. Replies: 5
    Last Post: 22nd September 2006, 08:04
  5. Qt 3.3.4 -> Qt 4.1.2, moc file issue
    By philski in forum Qt Tools
    Replies: 1
    Last Post: 11th September 2006, 21:08

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.