PyQt4 File Name Dialog Box

I'm a little newbie, so be kind ,-)

I had a graphical interface that I used with PyQt4 and python 2.6 with a working file dialog box (for example, you clicked a button and a window appeared and allowed you to choose a file to load / save). The code for the GUI is like 2,000 lines, so I will include the bits, which, in my opinion, are important:

from PyQt4 import QtGui as qt from PyQt4 import QtCore as qc class NuclearMotion(qt.QWidget): def __init__(self, parent=None): super(NuclearMotion, self).__init__(parent) file_button = qt.QPushButton("Use data from file") mainLayout = qt.QGridLayout() mainLayout.addWidget(file_button, 14, 8, 1, 2) def choose_file(): file_name = qt.QFileDialog.getOpenFileName(self, "Open Data File", "", "CSV data files (*.csv)") self.connect(file_button, qc.SIGNAL("clicked()"), choose_file) self.setLayout(mainLayout) if __name__ == '__main__': import sys app = qt.QApplication(sys.argv) NuclearMotionWidget = NuclearMotion() NuclearMotionWidget.show() sys.exit(app.exec_()) 

The above works absolutely fine. I typed all the code for this manually using various tutorials. Now I created a new GUI using the QT constructor and pyuic4 to convert it to a .py file. Now I can not make the file dialog work. In the code below, an error like:

 from PyQt4 import QtCore, QtGui class Ui_mainLayout(object): def setupUi(self, mainLayout): mainLayout.setObjectName(_fromUtf8("mainLayout")) mainLayout.resize(598, 335) mainLayout.setTabPosition(QtGui.QTabWidget.North) mainLayout.setTabShape(QtGui.QTabWidget.Rounded) mainLayout.setElideMode(QtCore.Qt.ElideLeft) self.basic_tab = QtGui.QWidget() self.file_button = QtGui.QPushButton(self.basic_tab) QtCore.QObject.connect(self.file_button, QtCore.SIGNAL("clicked()"), self.choose_file) def choose_file(self): file_name = QtGui.QFileDialog.getOpenFileName(self, "Open Data File", "", "CSV data files (*.csv)") if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) mainLayout = QtGui.QTabWidget() ui = Ui_mainLayout() ui.setupUi(mainLayout) mainLayout.show() sys.exit(app.exec_()) 

This code creates a graphical interface just fine, and everything else works fine, including the signals. Any idea what I'm doing wrong !?

+4
source share
1 answer

Your class must inherit (directly or indirectly) from QtCore.QObject in order to be able to process signals. The first inherits from QWidget, which does the job.

+2
source

All Articles