I've designed a UI using PyQT, and defined two windows (Main and Dialog). The idea is to define on the MainWindow several buttons and when you press on this it´s open a detail window with a list. I'm assuming that there's nothing wrong with the code created by pyuic4, but anyway I'll put the code of the Detail Window where you can see the list defined:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_DetailWindow(object):
def setupUi(self, DetailWindow):
DetailWindow.setObjectName(_fromUtf8("DetailWindow"))
DetailWindow.setWindowModality(QtCore.Qt.NonModal)
DetailWindow.setEnabled(True)
DetailWindow.resize(600, 650)
DetailWindow.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates))
self.btnClose = QtGui.QPushButton(DetailWindow)
self.btnClose.setEnabled(True)
self.btnClose.setGeometry(QtCore.QRect(480, 610, 110, 32))
self.btnClose.setObjectName(_fromUtf8("btnClose"))
self.label = QtGui.QLabel(DetailWindow)
self.label.setGeometry(QtCore.QRect(20, 20, 111, 20))
self.label.setObjectName(_fromUtf8("label"))
self.listAlarmsActive = QtGui.QListWidget(DetailWindow)
self.listAlarmsActive.setGeometry(QtCore.QRect(10, 50, 256, 192))
self.listAlarmsActive.setObjectName(_fromUtf8("listAlarmsActive"))
self.retranslateUi(DetailWindow)
QtCore.QMetaObject.connectSlotsByName(DetailWindow)
def retranslateUi(self, DetailWindow):
DetailWindow.setWindowTitle(_translate("DetailWindow", "Details", None))
self.btnClose.setText(_translate("DetailWindow", "Close", None))
self.label.setText(_translate("DetailWindow", "Alarms activated", None))
So, in the main code I define the class of the Detail window in this way:
class Detail(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ventanaDet = Ui_DetailWindow()
self.ventanaDet.setupUi(self)
self.connect(self.ventanaDet.btnClose, QtCore.SIGNAL('clicked()'), QtCore.SLOT('close()'))
So, in the constructor for the MainWindow, I do this:
class Principal(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ventana = Ui_MainWindow()
self.ventana.setupUi(self)
self.connect(self.ventana.btnExit, QtCore.SIGNAL('clicked()'), QtCore.SLOT('close()'))
self.ventana.btnHvac.clicked.connect(self.showDetailHvac)
self.ventana.btnCryo.clicked.connect(self.showDetailCryo)
self.ventanaDet = None
def showDetailHvac(self):
if self.ventanaDet is None:
self.ventanaDet = Detail()
self.ventanaDet.setWindowTitle("HVAC Alarms Detail")
alarmsHvac=[]
alarmsHvac.append("HVAC Alarm1")
alarmsHvac.append("HVAC Alarm2")
for alarm in alarmsHvac:
row=0
listItem=QtGui.QListWidgetItem(alarm);
self.ventanaDet.listAlarmsActive.insertItem(row,listItem)
row=row+1
self.ventanaDet.show()
This is the error I get on the console:
Traceback (most recent call last):
File "./aogAlarmPanel.py", line 37, in showDetailHvac
self.ventanaDet.listAlarmsActive.insertItem(row,listItem)
AttributeError: 'Detail' object has no attribute 'listAlarmsActive'
When I change the constructor (only for test purposes) and I put the data on it, it works fine, but is not the idea:
class Detail(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ventanaDet = Ui_DetailWindow()
self.ventanaDet.setupUi(self)
alarmsHvac=[]
alarmsHvac.append("HVAC Alarm1")
alarmsHvac.append("HVAC Alarm2")
for alarm in alarmsHvac:
row=0
listItem=QtGui.QListWidgetItem(alarm);
self.ventanaDet.listAlarmsActive.insertItem(row,listItem)
row=row+1
self.connect(self.ventanaDet.btnClose, QtCore.SIGNAL('clicked()'), QtCore.SLOT('close()'))
Can you help me please? I'm guessing that I have a huge error on the definitions of the windows, but I cannot find it. Thanks in advance.
You have made a very silly mistake here. You need to change:-
self.ventanaDet.listAlarmsActive.insertItem(row,listItem)
to
self.ventanaDet.ventanaDet.listAlarmsActive.insertItem(row,listItem)
.
You may have figured out whats wrong but still a small description of the bug is as:-
self.ventanaDet
is initialized with Details
and there is yet another variable ventanaDet
in Details
which is actually initialized with Ui_DetailWindow
.