moDong
moDong

Reputation: 233

Python pyqt - How do get value in a function from a custom dialog class

I'm new to pyqt and I'm trying to create a selector listwidgets that can be call by other functions, pass in items for user to pick and get the value from what user has picked from a function.

This is what I'm trying to do:

class SelectorDialog(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SelectorDialog, self).__init__(parent)

        QtGui.QWidget.__init__(self)
        self.setWindowFlags(QtCore.Qt.Dialog)
        self.setupUi(self)

        someItems = ['itemA', 'itemB', 'itemSomething']
        for item in someItems:
            list = QtGui.QListWidgetItem(item)
            self.listWidget.addItem(list)    
        #self.show()

    def setupUi(self, Form):
        Form.setWindowTitle('To which item?')
        self.verticalLayoutWidget = QtGui.QWidget(Form)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 500, 150))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.listWidget = QtGui.QListWidget(self.verticalLayoutWidget)
        self.verticalLayout.addWidget(self.listWidget)

        self.okButton = QtGui.QPushButton("OK")
        self.cancelButton = QtGui.QPushButton("Cancel")

        self.hbox = QtGui.QHBoxLayout(self.verticalLayoutWidget)
        self.hbox.addStretch(1)
        self.hbox.addWidget(self.okButton)
        self.hbox.addWidget(self.cancelButton)

        self.verticalLayout.addLayout(self.hbox)
        QtCore.QMetaObject.connectSlotsByName(Form)

        self.cancelButton.clicked.connect(lambda: self.close())
        self.okButton.clicked.connect(lambda: self.pick(self))  

    def pick(self, Form):
        for item in self.listWidget.selectedItems():
            picked = str(item.text())
        print picked



def whichItem():
    selector = SelectorDialog(parent)

    #find out picked item by SelecteorDialog, how ?

Upvotes: 0

Views: 1291

Answers (1)

user764357
user764357

Reputation:

First of all, if your object is a Dialog, its probably best to subclass from QDialog, not QWidget. This allows to to make use of the accept() and reject() methods of QDialog.

Caveat, I can't test the below at the moment, but its based on code I've written.

So in setupUi I'd make the following changes:

self.cancelButton.clicked.connect(self.reject) # Clicked cancel
self.okButton.clicked.connect(self.accept)  #Clicked accept

Then, when you call the dialog, you can check if the user actually "accepted" the dialog or cancelled it:

def whichItem():
    selector = SelectorDialog(parent)
    if selector.exec_() == QtGui.QDialog.Accepted:
        picks = selector.getPicks()

Then in the class definition make the following changes:

Class SelectorDialog(QtGui.QDialog):
    ...
    def getPicks(self):
        picked = []
        for item in self.listWidget.selectedItems():
            picked.append(str(item.text()))
        return picked

Upvotes: 1

Related Questions