Costis94
Costis94

Reputation: 250

PyQt: How to return a variable from a dialog function?

I my trying to retrieve a string variable for the selected path.

class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):

def __init__(self):

    super(self.__class__, self).__init__()
    self.setupUi(self)  

    A = self.in_browse_button.clicked.connect(self.browser)

def browser(self):
    global directory
    directory = str(QFileDialog.getExistingDirectory())
    self.input_edit.setText(directory)
    return directory

The dialog open when I press the button but when I try to use A variable it's value is None. Any idea?

Upvotes: 0

Views: 1064

Answers (1)

Matho
Matho

Reputation: 305

This code

A = self.in_browse_button.clicked.connect(self.browser)

does not assign the result of self.browser but result of clicked signal to the variable A. According to the documentation, signal clicked does not return anything. Therefore, since you are in a class, I recommend defining an attribute for the class and store result of QFileDialog in there.

class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.directory = None
        self.in_browse_button.clicked.connect(self.browser)

    def browser(self):
        self.directory = str(QFileDialog.getExistingDirectory())
        self.input_edit.setText(directory)

Upvotes: 1

Related Questions