[PyQt] File Dialog Opens Twice

David Arnold dwarnold45 at suddenlink.net
Sat Feb 13 20:02:18 GMT 2010


David, Detlev,

OK, I am gonna guess that this line in setupUi is what you are both referring to:

       QtCore.QMetaObject.connectSlotsByName(Notepad)

Here is what I was thinking:

        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), Notepad.close)
        QtCore.QMetaObject.connectSlotsByName(Notepad)

I thought the first line took care of my close button, so I would have to take care of the open button in my file.

The following archive makes it even more confusing for me:

http://msemac.redwoods.edu/~darnold/junk/Archive.zip

In test.py (generated from edytor.ui with pyuic4 edytor.ui>test.py) I have the same connections in setupUi:

        self.retranslateUi(Notepad)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), Notepad.close)
        QtCore.QMetaObject.connectSlotsByName(Notepad)

Yet in my test_start.py, I write:

        # here we connect signals with our slots
        QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"),self.file_dialog)
            
    def file_dialog(self):
        fd = QtGui.QFileDialog(self)
        plik = open(fd.getOpenFileName()).read()
        self.ui.editor_window.setText(plik)

And I don't get the double open problem when I run test_start.py.

I don't understand the difference.

Can you explain?

David.


On Feb 12, 2010, at 8:13 PM, David Boddie wrote:

> 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
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list