2 Attachment(s)
pyqt4 dialog "object has no attribute"
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 Attachment 12188Attachment 12189.
Using Qt 5.4 , pyqt4, python3
Re: pyqt4 dialog "object has no attribute"
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,
_
Re: pyqt4 dialog "object has no attribute"
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.
Re: pyqt4 dialog "object has no attribute"
Can you post your updated code?
Cheers,
_
Re: pyqt4 dialog "object has no attribute"
Updated Code:
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):
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
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(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():
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_())
Re: pyqt4 dialog "object has no attribute"
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,
_
Re: pyqt4 dialog "object has no attribute"
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'
Re: pyqt4 dialog "object has no attribute"
Quote:
Originally Posted by
jacksonr
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,
_
Re: pyqt4 dialog "object has no attribute"