Home > database >  How to select current items in pyqt5 QlistWidget
How to select current items in pyqt5 QlistWidget

Time:09-23

Im using qt designer and have a qlistwidget populated from a db. I want to be able to select from the list and at the moment just print it out. Im using the currentItem method but its not returning a string. It returns only the memory location. I don't understand how to get this to work properly.

        session_name_=str(self.listWidget.currentItem())

        addendant_=self.listWidget_2.currentRow()
        scheduled_=self.dateTimeEdit.dateTime()
        sched=scheduled_.toString("MM/dd/yyyy h:mm ap")
        
        print(session_name_)

It returns <PyQt5.QtWidgets.QListWidgetItem object at 0x7fe5050bccd0>

CodePudding user response:

Try to use: session_name = self.listWidget.currentItem().text()

CodePudding user response:

If you need to get all the selected items, you have to use the view's selectionModel(). The selectedIndexes() returns a list of unique QModelIndexes which is not sorted:

selection = self.listWidget.selectionModel()
indexes = sorted(selection.selectedIndexes(), key=lambda i: i.row())
for index in indexes:
    print(index.data())
  • Related