PDA

View Full Version : pyqt4 dialog "object has no attribute"



jacksonr
21st October 2016, 15:41
I am launching a dialog box containing a form with several input fields. I want to enter data in the first field and then execute a lookup for a description and display that data in the second (read only field). To this end I have created the dialog in QTDesigner and written a simple main that launches the dialog on a button press. The dialog issues a signal when data is entered in the first field. The dialog is sub-classed and has a slot that receives the signal. My problem is that when the slot method runs it can't access the dialogs' fields and the following error is generated.


Traceback (most recent call last):
File "/Users/robert/PycharmProjects/pcbMrp/Utilities/dialogTest.py", line 15, in showPartDescription
part = self.pcePartNoEditor.text()
AttributeError: 'PartCountEditorDialog' object has no attribute 'pcePartNoEditor'

How do I get the subclass method (slot) to access the dialog elements?

Example 1218812189.

Using Qt 5.4 , pyqt4, python3

anda_skoa
21st October 2016, 18:21
Your dialog should inherit QDialog and use the "UI" class as a member.

Then any mehod or slot in your class can access all UI elements through the "UI" member.

Cheers,
_

Your dialog should inherit QDialog and use the "UI" class as a member.

Then any mehod or slot in your class can access all UI elements through the "UI" member.

Cheers,
_

jacksonr
21st October 2016, 20:25
The subclass: "class PartCountEditorDialog(QDialog, Ui_PartCountEditDialog):" inherits QDialog and Ui_PartCountEditDialog.

If in the "QDialog.__init__((self))" I call "self.setupUi(self)" then I don't throw the error but, in the slot method, the statements: "part = self.pcePartNoEditor.text()" and "self.pcePartDescription.setText(partDesc)" execute but don't do anything. I can't retrieve data from the dialog field or set data in the field.

anda_skoa
22nd October 2016, 10:37
Can you post your updated code?

Cheers,
_

jacksonr
25th October 2016, 00:46
Updated Code:



import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSlot
from Source.partCount import refreshCountTable, PartCountEditorDialog
from Resources.CountScreenEditorDialog import Ui_PartCountEditDialog


class PartCountEditorDialog(QDialog, Ui_PartCountEditDialog):
def __init__(self):
QDialog.__init__((self))
self.setupUi(self)
# self.ui = Ui_PartCountEditDialog


@pyqtSlot()
def showPartDescription(self):
part = self.pcePartNoEditor.text()
print(part)
partDesc = 'A Part'
self.pcePartDescription.setText(partDesc)
self.repaint()
pass

class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()

def initUI(self):
qbtn = QtGui.QPushButton('Enter Count', self)
qbtn.clicked.connect(lambda: self.launchPcDialog())
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50)

self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Dialog Test')
self.show()

@pyqtSlot()
def launchPcDialog(self):
PartCountEditor = PartCountEditorDialog() # Create an edit dialog
pcUi = Ui_PartCountEditDialog()
pcUi.setupUi(PartCountEditor)
pcUi.pceDeleteWarning.hide() # This is not a delete so hide the delete message
result = PartCountEditor.exec_() # Launch the Part Count Edit Dialog. result = 0 for Cancel, 1 for Save


#
# MAIN WINDOW
#
# sub class MainWindow
class Main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

#
# def __init__(self):
# QtGui.QMainWindow.__init__(self)
# self.ui = Ui_MainWindow()
# self.ui.setupUi(self)
#
#
#
# app = QtGui.QApplication(sys.argv)
#
# MainWindow = Main()
#
# MainWindow.show()
#
# sys.exit(app.exec_())

anda_skoa
25th October 2016, 09:00
In "launchPcDialog" you are creating an instance of PartCountEditorDialog, which creates the UI elements, and then you create another set of UI elements that the dialog has no access to.

So the code in your showPartDescription slot accesses the first set of elements, which is no longer visible since they are hidden by the second second set.

Just don't create the second set.

Cheers,
_

jacksonr
26th October 2016, 14:41
If I comment out line 12 in the above code, I am back to the original problem:

File "/Users/robert/PycharmProjects/pcbMrp/Utilities/dialogTest.py", line 18, in showPartDescription
part = self.pcePartNoEditor.text()
AttributeError: 'PartCountEditorDialog' object has no attribute 'pcePartNoEditor'

anda_skoa
26th October 2016, 16:21
If I comment out line 12 in the above code, I am back to the original problem:

And why on earth would you do that?

Why would you want to not create the UI elements that you need and keep the ones you don't?

Cheers,
_

jacksonr
27th October 2016, 19:15
I get it now.

Thanks