I am a bit of a novice so be kind ;-)
I had a GUI that I made using PyQt4 and python 2.6 with a working file dialog, (ie you clicked a button and a window popped up and allowed you to pick a file to load/save). The code for the GUI is like 2000 lines, so i will include the bits i think 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 it manually using various tutorials. I have now made a new GUI using QT designer and pyuic4 to convert it to a .py file. Now I can't make the file dialog work. The below code results in a Type error:
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 produces the GUI just fine and everything else works ok including signals. Any idea what I am doing wrong!?
Your class should inherit (directly or indirectly) from QtCore.QObject to be able to handle signals. The first one inherits from QWidget, which does the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With