Hi!
Using PyQt5, I'm trying to get a custom dialog (containing a simple QListWidget) to return a value.

After much tweaking and looking online, I still can't find what's wrong with this code.

The custom dialog is in this class:

class ListSelection(QDialog):
def __init__(self, item_ls, parent=None):
super(ListSelection, self).__init__(parent)
self.result = ""
#================================================= ===============================
# listbox
#================================================= ===============================
self.listWidget = QListWidget()
for item in item_ls:
w_item = QListWidgetItem(item)
self.listWidget.addItem(w_item)
self.listWidget.itemClicked.connect(self.OnSingleC lick)
self.listWidget.itemActivated.connect(self.OnDoubl eClick)
layout = QGridLayout()
row=0
layout.addWidget(self.listWidget,row,0,1,3) #col span=1, row span=3
#================================================= ===============================
# OK, Cancel
#================================================= ===============================
row +=1
self.but_ok = QPushButton("OK")
layout.addWidget(self.but_ok ,row,1)
self.but_ok.clicked.connect(self.OnOk)

self.but_cancel = QPushButton("Cancel")
layout.addWidget(self.but_cancel ,row,2)
self.but_cancel.clicked.connect(self.OnCancel)

#================================================= ===============================
#
#================================================= ===============================
self.setLayout(layout)
self.setGeometry(300, 200, 460, 350)


def OnSingleClick(self, item):
self.result = item.text()


def OnDoubleClick(self, item):
self.result = item.text()
self.close()
return self.result


def OnOk(self):
if self.result == "":
QMessageBox.information(self, "Error",
"One item must be selected")
return
self.close()
return self.result


def OnCancel(self):
self.close()


def GetValue(self):
return self.result


And this is what the calling function does:


def SomeFunction()
ls = ['apples','bananas','melons']
lb = ListSelection(ls)
if lb.exec_():
value = lb.GetValue()
print(value)


The problem is, this does not capture any value.

Thanks!