Results 1 to 2 of 2

Thread: PYQT4 QlistWidget : currentItem and selectedItem different

  1. #1
    Join Date
    Sep 2016
    Qt products

    Default Re: PYQT4 QlistWidget : currentItem and selectedItem different

    Hi Everyone,

    So I curious to know how does the currentItem()) and selectedItems() methods of the QlistWidget actually work.

    What I have(simplified):
    -I have a QlistWidget with a list of items(usersList)
    -I have QAbstractViewItem.extendedSelection activated
    -I have QLineEdit(userAge)
    -I have a dictionary DataDict with key:value ==> ListItem.text():QlineEdit.text())

    -I have connected the userAge.textChanged to the function saveData

    def saveData():


    -I have connected usersList.currentItemChanged to the function loadData:

    def loadData(current,_previous):
    #get data
    #clear previous data
    #set Data

    what I want to do/problem(simplified):

    -right now the saving/loading of the data in the UI works for one item
    (currentitem) but I want the user to be able to select multiple Items
    change the usersAge and setData to all the selectedItems , and I'm trying to do this by modifying my saveData() like so:

    def saveData():
    for item in usersList.selectedItems():

    -the problem now is that when I changed currentItem by clicking on another one item the data of the previous gets overwritten by the current loaded one

    -I know whats probably happening is (if i'm not wrong):
    1.I click in another item(changing current Item)
    2.currentItemChanged is triggered
    3.loadData() is called
    3.a userAge.clear() is called
    3.b userAge.textChanged is triggered
    3.c saveData() is called (now with empty)
    3.d userAge.setText(data)
    3.e userAge.textChanged is triggered
    3.f saveData() is called(with the loaded data)

    -In my head it should work fine because currentItem/selectedItem are the same, but what is causing the problem is that when saveData() is called , the currentItem and the selectedItem are different, if I print currentItem.text() and selectedItems()[0].text() when I entered savData()

    I noticed that currentItem= currentItem (which is correct) but selectedItems()[0].text() = previousItem (which is wrong cause only the currentItem is selected)

    why is this happening? what am I doing wrong? or what am I missing?
    why is the selection not getting updated when you changed current Item?

    I a newbie so I might be doing something wrong , but if not could someone please help me understand what is happening?

    thank you very much in advance

    so after playing around and trying some different stuff (not logical just trying) I found a solution to this problem , so what I have to do is called the usersList.setCurrentItem(current) inside the loadData() function like so:

    def loadData(current,_previous):
    #get data
    #clear previous data
    #set Data

    this seems very redundant to me , but by doing this, when I print currentItem and selectedItems()[0] (when only one item selected) in saveData() it prints currentItem==selectedItems()[0] , so the onlye data getting overwritten is the currentItem,selectedItems()[0] which is own data which give me the behaviour I want

    but I'm still clueless why this happens and why this fixes the issue

    Last edited by alkimia; 3rd January 2017 at 22:08.

  2. #2
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: PYQT4 QlistWidget : currentItem and selectedItem different

    You also don't necessarily have to call "clear()" on userAge, "setText()" will overwrite the value anyway.

    Also, one technique that is sometimes useful to handle change by user and change by program differently is to disable signals when changing a component programatically.

    Qt Code:
    1. def loadData(current)
    2. # ....
    3. userAge.blockSignals(true)
    4. userAge.setText(current)
    5. userAge.blockSignals(false)
    To copy to clipboard, switch view to plain text mode 
    Here setText() will not result in textChanged() as signals had been blocked, so loadData() does not trigger saveData()


Similar Threads

  1. Replies: 2
    Last Post: 19th March 2014, 09:23
  2. (PyQt4) QtGui QListWidget adding new item problem
    By mshemuni in forum Qt Programming
    Replies: 0
    Last Post: 4th September 2012, 04:05
  3. Need help setting the QTreeView selectedItem color
    By loren_rogers in forum Newbie
    Replies: 1
    Last Post: 23rd November 2011, 11:15
  4. QListWidget::currentItem() issue
    By high_flyer in forum Qt Programming
    Replies: 4
    Last Post: 16th April 2007, 11:04
  5. what is currentItem?
    By Alina in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2006, 06:35

Tags for this Thread


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.