Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Some .ui files/elements not loading in Python
Forum Updated to NodeBB v4.3 + New Features

Some .ui files/elements not loading in Python

Scheduled Pinned Locked Moved Unsolved Qt for Python
python
3 Posts 3 Posters 584 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anthnyshark
    wrote on 17 Sept 2024, 18:29 last edited by
    #1

    I have 2 almost identical .ui files, but one of them isn't loading. I can't tell a significant difference in the files, but one will load the actual screen designed in Qt Designer, but one will just output an error (I need it to load the one that doesn't currently work). Here is the main code, and the errors that I get when I try to load the non-functioning file. I want to load this file as the layout and formatting is nicer than the other.

    import sys
    from PyQt5.uic import loadUi
    from PyQt5 import *
    from PyQt5.QtWidgets import *
    
    
    class WelcomeScreen(QDialog):
        def __init__(self):
            super(WelcomeScreen, self).__init__()
            loadUi('test.ui', self)
            self.show()
    
    
    # main code block
    app = QApplication(sys.argv)
    welcome = WelcomeScreen()
    widget = QStackedWidget()
    widget.addWidget(welcome)
    widget.setFixedSize(480, 640)
    widget.show()
    
    
    try:
        sys.exit(app.exec_())
    except ():
        print('Exiting')
    

    and here is the error message I get when loading the .ui file

    Traceback (most recent call last):
      File "C:\Users\socks\Desktop\realLogin\pythonProject\main.py", line 16, in <module>
        welcome = WelcomeScreen()
                  ^^^^^^^^^^^^^^^
      File "C:\Users\socks\Desktop\realLogin\pythonProject\main.py", line 10, in __init__
        loadUi('test.ui', self)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\__init__.py", line 241, in loadUi
        return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\Loader\loader.py", line 66, in loadUi
        return self.parse(filename, resource_suffix)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 1037, in parse
        actor(elem)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 828, in createUserInterface
        self.traverseWidgetTree(elem)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
        handler(self, child)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 273, in createWidget
        self.traverseWidgetTree(elem)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
        handler(self, child)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 273, in createWidget
        self.traverseWidgetTree(elem)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
        handler(self, child)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 503, in createLayout
        self.traverseWidgetTree(elem)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
        handler(self, child)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 546, in handleItem
        self.traverseWidgetTree(elem)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
        handler(self, child)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 264, in createWidget
        self.stack.push(self.setupObject(widget_class, parent, elem))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 230, in setupObject
        self.wprops.setProperties(obj, branch)
      File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\properties.py", line 417, in setProperties
        getattr(widget, 'set%s%s' % (ascii_upper(prop_name[0]), prop_name[1:]))(prop_value)
    TypeError: setWhatsThis(self, a0: Optional[str]): argument 1 has unexpected type 'int'
    
    Process finished with exit code 1
    
    

    Another recurring issue is not being able to use text alignment, giving me errors like :

    AttributeError: type object 'Qt' has no attribute 'Qt::AlignmentFlag::AlignCenter'
    
    J 1 Reply Last reply 17 Sept 2024, 18:38
    0
    • A anthnyshark
      17 Sept 2024, 18:29

      I have 2 almost identical .ui files, but one of them isn't loading. I can't tell a significant difference in the files, but one will load the actual screen designed in Qt Designer, but one will just output an error (I need it to load the one that doesn't currently work). Here is the main code, and the errors that I get when I try to load the non-functioning file. I want to load this file as the layout and formatting is nicer than the other.

      import sys
      from PyQt5.uic import loadUi
      from PyQt5 import *
      from PyQt5.QtWidgets import *
      
      
      class WelcomeScreen(QDialog):
          def __init__(self):
              super(WelcomeScreen, self).__init__()
              loadUi('test.ui', self)
              self.show()
      
      
      # main code block
      app = QApplication(sys.argv)
      welcome = WelcomeScreen()
      widget = QStackedWidget()
      widget.addWidget(welcome)
      widget.setFixedSize(480, 640)
      widget.show()
      
      
      try:
          sys.exit(app.exec_())
      except ():
          print('Exiting')
      

      and here is the error message I get when loading the .ui file

      Traceback (most recent call last):
        File "C:\Users\socks\Desktop\realLogin\pythonProject\main.py", line 16, in <module>
          welcome = WelcomeScreen()
                    ^^^^^^^^^^^^^^^
        File "C:\Users\socks\Desktop\realLogin\pythonProject\main.py", line 10, in __init__
          loadUi('test.ui', self)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\__init__.py", line 241, in loadUi
          return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\Loader\loader.py", line 66, in loadUi
          return self.parse(filename, resource_suffix)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 1037, in parse
          actor(elem)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 828, in createUserInterface
          self.traverseWidgetTree(elem)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
          handler(self, child)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 273, in createWidget
          self.traverseWidgetTree(elem)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
          handler(self, child)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 273, in createWidget
          self.traverseWidgetTree(elem)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
          handler(self, child)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 503, in createLayout
          self.traverseWidgetTree(elem)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
          handler(self, child)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 546, in handleItem
          self.traverseWidgetTree(elem)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 806, in traverseWidgetTree
          handler(self, child)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 264, in createWidget
          self.stack.push(self.setupObject(widget_class, parent, elem))
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\uiparser.py", line 230, in setupObject
          self.wprops.setProperties(obj, branch)
        File "C:\Users\socks\Desktop\realLogin\pythonProject\.venv\Lib\site-packages\PyQt5\uic\properties.py", line 417, in setProperties
          getattr(widget, 'set%s%s' % (ascii_upper(prop_name[0]), prop_name[1:]))(prop_value)
      TypeError: setWhatsThis(self, a0: Optional[str]): argument 1 has unexpected type 'int'
      
      Process finished with exit code 1
      
      

      Another recurring issue is not being able to use text alignment, giving me errors like :

      AttributeError: type object 'Qt' has no attribute 'Qt::AlignmentFlag::AlignCenter'
      
      J Offline
      J Offline
      JonB
      wrote on 17 Sept 2024, 18:38 last edited by
      #2

      @anthnyshark said in Some .ui files/elements not loading in Python:

      I have 2 almost identical .ui files, but one of them isn't loading. I can't tell a significant difference in the files

      Use a "differencer" tool to compare their content.

      AttributeError: type object 'Qt' has no attribute 'Qt::AlignmentFlag::AlignCenter'

      This is to do with PyQt5/6. At 5 it may have been Qt::AlignCenter. Are you using a very recent version of Designer/Creator? I think the .ui files it generates may not be backwardly compatible to PyQt5 now. If that is so might be the cause of your problems/different file behaviour.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 17 Sept 2024, 19:38 last edited by
        #3

        Hi,

        One strange thing is that the error shown is invalid Python. As @JonB suggested, check the exact difference between the two files. Especially if you set this property.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        1/3

        17 Sept 2024, 18:29

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved