0

In QT Designer I have defined a button loadValues_btn and associated the signal pressed() to the custom slot loadValues()

Then in Python, after having loaded the .ui file with the PyQt library, I have defined the method loadValues() in the MainApplication class, in order to execute custom code when the same button is clicked. But it does not execute the code (I need simply to open a File chooser dialog)

I have already tried with clicked() and it didn't work either

Here is the MainApplication class code:

class Main(QMainWindow, Ui_MainWindow):    
    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)

   def loadValues():
       filePath = QtGui.QFileDialog.getOpenFileName(parent=self, caption='Open file', directory='.')

if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)
   main = Main()
   main.show()
   sys.exit(app.exec_())
davide
  • 221
  • 3
  • 16
  • `setupUi` should do the connection. If it does not, then you can try to fix it on Designer (make sure you have correct connections defined), or you can just do the connect in code yourself. – hyde Sep 26 '15 at 07:27
  • @hyde I have generated with PyQt4 the python code behind the UI interface, and it seems to me that signals and slots are correctly connected for the loadValues slot... – davide Sep 26 '15 at 09:52
  • here is the code present in setpUi of the Ui_MainWindow class: 'class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(803, 476) ... self.loadData_btn = QtGui.QPushButton(self.centralWidget) self.loadData_btn.setGeometry(QtCore.QRect(30, 10, 75, 23)) self.loadData_btn.setObjectName(_fromUtf8("loadData_btn")) ... QtCore.QObject.connect(self.loadData_btn, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.loadValues) ... ' I cannot understand where the error is :( – davide Sep 26 '15 at 09:53
  • I don't know much about PyQt, but check console output (stderr) of the application for connect failure messages, and try adding debug print telling the return value of connect (should be true). Then you could edit the question to add the stuff in above comment (and format it nicely), so it's easier for somebody else to look at. – hyde Sep 26 '15 at 10:43
  • The code you posted has an indentation error, and it will also raise a `TypeError` because `loadValues` has a missing `self` parameter. Other than that, there does not appear to be anything wrong. (PS: don't forget to re-run `pyuic` if you make changes in Qt Designer). – ekhumoro Sep 26 '15 at 16:09

1 Answers1

1

Thank you all for your support. Actually the File dialog was not appearing simply because... it was behind the Spyder editor window!! So it was hidden and it did not pop up in the main window of the Python application I was developing!!! :( Useless to tell you that I have lost one day to understand where the problem was, f***ing Python!!!

davide
  • 221
  • 3
  • 16