Results 1 to 3 of 3

Thread: PyQt custom dialog -- how to get the return value?

  1. #1
    Join Date
    Apr 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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!

  2. #2
    Join Date
    Apr 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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:

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt custom dialog -- how to get the return value?

    Quote Originally Posted by Karpov View Post
    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,
    _

Similar Threads

  1. pyqt New Dialog No Attribute exec_
    By supertom44 in forum Newbie
    Replies: 2
    Last Post: 10th November 2016, 18:58
  2. Replies: 4
    Last Post: 27th October 2014, 03:34
  3. Get (custom) return value of modal dialog?
    By Terreox in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2012, 15:20
  4. Multi-dialog program in PyQT will not close
    By Danny Hatt in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2010, 22:12
  5. Multi-dialog program in PyQT will not close (the sequel!)
    By Danny Hatt in forum Qt Programming
    Replies: 0
    Last Post: 19th June 2010, 01:05

Tags for this Thread

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.