[PyQt] File Dialog Opens Twice

David Boddie david at boddie.org.uk
Sat Feb 13 04:13:22 GMT 2010


On Sat Feb 13 02:07:44 GMT 2010, David Arnold wrote:

[...]

> class Notepad(QMainWindow, Ui_Notepad):
>   def __init__(self, parent = None):
>       QMainWindow.__init__(self, parent)
>       self.setupUi(self)

^^^ Connection 1 (behind the scenes)

>       self.connect(self.button_open,
>                              SIGNAL("clicked()"), 
>                              self.on_button_open_clicked)

^^^ Connection 2

>   @pyqtSignature("")
>   def on_button_open_clicked(self):
>       fd=QFileDialog(self)
>       plik=open(fd.getOpenFileName()).read()
>       self.editor_window.setText(plik)
>
>
> However, when I run the project and click on the Open button, the file
> dialog opens, I select start.py in the SimpleTextEditor folder, press OK
> and it opens fine in the QTextEdit window. However, then the QFileDialog
> opens a second time. Weird.
>
> Can anyone explain what I am doing wrong?

You called setupUi(self) which connects all the signals to specially-named
slots like the one you defined (on_button_open_clicked) and decorated with
@pyqtSignature. Then you explicitly connected the push button's signal to
the slot again.

So, when the button is pressed, the slot will be invoked twice and the
dialog will be opened twice.

David


More information about the PyQt mailing list