PyQt custom dialog -- how to get the return value?
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!
Re: PyQt custom dialog -- how to get the return value?
Okay. So no one wastes time here, the answer is that lb.exec_() returns 0, so the condition in SomeFunction() is never evaluated.
The way to do it is, in the dialog class, to replace QDialog.close() by QDialog.done(int)
Then in SomeFunction, test for:
if lb.exec_() == QDialog.Accepted:
Re: PyQt custom dialog -- how to get the return value?
Quote:
Originally Posted by
Karpov
The way to do it is, in the dialog class, to replace QDialog.close() by QDialog.done(int)
Or to call accept() or reject().
Usually one would do that instead of the OnOk and OnCancel slots, i.e. connect the OK button to accept() and the Cancel button to reject(), overwriting either method if something special needs to be done there (like checking for valid values).
Also one would usually use QDialogButtonBox to provide the buttons, in order to make sure that button order is correct on the platform the program runs on.
Cheers,
_