Results 1 to 9 of 9

Thread: pyqt4 dialog "object has no attribute"

  1. #1
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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 CountScreenEditorDialog.pydialogTest.py.

    Using Qt 5.4 , pyqt4, python3

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: pyqt4 dialog "object has no attribute"

    Can you post your updated code?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: pyqt4 dialog "object has no attribute"

    Updated Code:

    Qt Code:
    1. import sys
    2. from PyQt4 import QtCore, QtGui
    3. from PyQt4.QtGui import *
    4. from PyQt4.QtCore import pyqtSlot
    5. from Source.partCount import refreshCountTable, PartCountEditorDialog
    6. from Resources.CountScreenEditorDialog import Ui_PartCountEditDialog
    7.  
    8.  
    9. class PartCountEditorDialog(QDialog, Ui_PartCountEditDialog):
    10. def __init__(self):
    11. QDialog.__init__((self))
    12. self.setupUi(self)
    13. # self.ui = Ui_PartCountEditDialog
    14.  
    15.  
    16. @pyqtSlot()
    17. def showPartDescription(self):
    18. part = self.pcePartNoEditor.text()
    19. print(part)
    20. partDesc = 'A Part'
    21. self.pcePartDescription.setText(partDesc)
    22. self.repaint()
    23. pass
    24.  
    25. class Example(QtGui.QWidget):
    26. def __init__(self):
    27. super(Example, self).__init__()
    28. self.initUI()
    29.  
    30. def initUI(self):
    31. qbtn = QtGui.QPushButton('Enter Count', self)
    32. qbtn.clicked.connect(lambda: self.launchPcDialog())
    33. qbtn.resize(qbtn.sizeHint())
    34. qbtn.move(50, 50)
    35.  
    36. self.setGeometry(300, 300, 250, 150)
    37. self.setWindowTitle('Dialog Test')
    38. self.show()
    39.  
    40. @pyqtSlot()
    41. def launchPcDialog(self):
    42. PartCountEditor = PartCountEditorDialog() # Create an edit dialog
    43. pcUi = Ui_PartCountEditDialog()
    44. pcUi.setupUi(PartCountEditor)
    45. pcUi.pceDeleteWarning.hide() # This is not a delete so hide the delete message
    46. result = PartCountEditor.exec_() # Launch the Part Count Edit Dialog. result = 0 for Cancel, 1 for Save
    47.  
    48.  
    49. #
    50. # MAIN WINDOW
    51. #
    52. # sub class MainWindow
    53. class Main():
    54. app = QtGui.QApplication(sys.argv)
    55. ex = Example()
    56. sys.exit(app.exec_())
    57.  
    58.  
    59. if __name__ == '__main__':
    60. main()
    61.  
    62. #
    63. # def __init__(self):
    64. # QtGui.QMainWindow.__init__(self)
    65. # self.ui = Ui_MainWindow()
    66. # self.ui.setupUi(self)
    67. #
    68. #
    69. #
    70. # app = QtGui.QApplication(sys.argv)
    71. #
    72. # MainWindow = Main()
    73. #
    74. # MainWindow.show()
    75. #
    76. # sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  7. #7
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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'

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: pyqt4 dialog "object has no attribute"

    Quote Originally Posted by jacksonr View Post
    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,
    _

  9. #9
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: pyqt4 dialog "object has no attribute"

    I get it now.

    Thanks

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. Replies: 0
    Last Post: 23rd April 2014, 07:45
  3. PyQt4: QNetworkDiskCache. Save images with attribute "no-cache"
    By rustamakhmetov in forum Qt Programming
    Replies: 0
    Last Post: 25th November 2011, 05:50
  4. "Render" Qt dialog window from ".ui" file
    By BitEater in forum Qt Programming
    Replies: 1
    Last Post: 8th July 2011, 14:40
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.