Hello everyone,

I am attaching 2 sample codes - one that doesn't work and another that does. There is no apparent reason for the earlier not to work. [The third script main.py just defines the interface and is imported in both cases.]

core_notworking.py
To illustrate my problem, I have a line-edit (Edt1, has the text "Choose") and a pushbutton (Btn1, labelled "Choose"), both of which are trying to open a file dialog box. [The other pushbutton is merely to quit the application.] In the first case, pressing the return key from the line edit does open a file dialog box. The user can then choose the file, the name is displayed correctly in the line-edit and the file dialog box closes. In the second case, the file dialog box opens and the name of the file the user chooses is correctly printed on the command-line. However, the file dialog box doesn't close and we seem to hit an infinite loop. If we forcibly close the application, the file name is lost.

core_working.py
I replicate the situation described above and add @pyqtSlot descriptors as described at http://www.riverbankcomputing.com/static/Docs/PyQt4/html/new_style_signals_slots.html

I am not sure why omitting the descriptor completely alters the expected behavior. It would be great if someone could please help me with this.

Thanks in advance,
Jishnu

#!/usr/bin/env python
from PyQt4 import QtGui
import re
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import main
import sys
import os

class MainWindow(QMainWindow, main.Ui_MainWindow):

  
  def __init__(self, parent=None):
    super(MainWindow, self).__init__(parent)
    self.setupUi(self)

  @pyqtSlot()
  def on_Edt1_returnPressed(self):
    fname1 = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 
                '/home')
    self.Edt1.clear()
    self.Edt1.setText(fname1)

  @pyqtSlot()
  def on_Btn1_clicked(self):
    fname2 = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 
                '/home')
    print fname2
 
  @pyqtSlot()
  def on_Quit_clicked(self):
    sys.exit()

if __name__ == "__main__":
   
  app = QApplication(sys.argv)
  ex = MainWindow()
  ex.show()
  app.exec_()
  
  
  
  
  
  
  

#!/usr/bin/env python
from PyQt4 import QtGui
import re
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import main
import sys
import os

class MainWindow(QMainWindow, main.Ui_MainWindow):

  def __init__(self, parent=None):
    super(MainWindow, self).__init__(parent)
    self.setupUi(self)

  def on_Edt1_returnPressed(self):
    fname1 = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 
                '/home')
    self.Edt1.clear()
    self.Edt1.setText(fname1)

  def on_Btn1_clicked(self):
    fname2 = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 
                '/home')
    print fname2
 
  def on_Quit_clicked(self):
    sys.exit()

if __name__ == "__main__":
   
  app = QApplication(sys.argv)
  ex = MainWindow()
  ex.show()
  app.exec_()
  
  
  
  
  
  
  

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(835, 644)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setEnabled(True)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.Quit = QtGui.QPushButton(self.centralwidget)
        self.Quit.setGeometry(QtCore.QRect(170, 380, 75, 25))
        self.Quit.setObjectName(_fromUtf8("Quit"))
        self.Btn1 = QtGui.QPushButton(self.centralwidget)
        self.Btn1.setGeometry(QtCore.QRect(130, 310, 156, 25))
        self.Btn1.setObjectName(_fromUtf8("Btn1"))
        self.Edt1 = QtGui.QLineEdit(self.centralwidget)
        self.Edt1.setGeometry(QtCore.QRect(110, 200, 205, 21))
        self.Edt1.setObjectName(_fromUtf8("Edt1"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.Quit.setText(QtGui.QApplication.translate("MainWindow", "Quit", None, QtGui.QApplication.UnicodeUTF8))
        self.Btn1.setText(QtGui.QApplication.translate("MainWindow", "Choose", None, QtGui.QApplication.UnicodeUTF8))
        self.Edt1.setText(QtGui.QApplication.translate("MainWindow", "Choose", None, QtGui.QApplication.UnicodeUTF8))
  
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to