Krin123
Krin123

Reputation: 343

PyQt clicked.connect is connected automatically

I'm currently implementing a model view architecture for my PyQt GUI. Here's a simpler, but representative version of my code at the moment (since mine is waaaay too long)

class Model(QtGui.QWidget):
    def __init__(self):
        self.openDir = '/some/file/dir/'
    def openFile(self):
        openFileName = QtGui.QFileDialog.getOpenFileName(None, "Open File",self.loadDir,"AllFiles(*.*)")
        open = open(openFileName, 'r')
        ...

class View(QtGui.QWidget):
    def__init__(self):
    ...
    self.button = QtGui.QPushButton("Open")
    ...
    self.button.clicked.connect(Model().openFile())

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mainWindow = View()
    mainWindow.show()
    sys.exit(app.exec_())

However, the even if I haven't pressed the button, the signal is already emitted and the QFileDialog window comes up automatically.


Edit 1:

Because I ran into a new problem regarding the same subject, I have opened a new question for more input.

Upvotes: 1

Views: 1948

Answers (1)

Fred S
Fred S

Reputation: 995

I think that I see the issue.

self.button.clicked.connect(Model().openFile())

should be

self.button.clicked.connect(Model().openFile)

In the first instance, you're calling the openFile method and passing the return value to "connect". In the the second, you're passing the method itself to "connect".

Upvotes: 2

Related Questions