Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't see image from resource in python
Forum Update on Monday, May 27th 2025

Can't see image from resource in python

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 391 Views
  • 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.
  • X Offline
    X Offline
    Xav12358
    wrote on 9 Sept 2021, 13:51 last edited by
    #1

    Hello,

    I try to use QTDesign and integrate a image in a QLabel. So I create a .qrc and add my image:

    ac533fbe-a9c1-4aa2-8308-0bb27f75605a-image.png

    I the designer tool I see my image but not in my program:
    In designer tool
    6c6616b2-b3a6-46dd-a360-ab7e1b9b9b31-image.png

    I set the scalecontents. Here is the python code I use ( the default program)

    # This Python file uses the following encoding: utf-8
    import os
    from pathlib import Path
    import sys
    
    from PySide2.QtWidgets import QApplication, QWidget
    from PySide2.QtCore import QFile
    from PySide2.QtUiTools import QUiLoader
    
    
    class Widget(QWidget):
        def __init__(self):
            super(Widget, self).__init__()
            self.load_ui()
    
        def load_ui(self):
            loader = QUiLoader()
            path = os.fspath(Path(__file__).resolve().parent / "form.ui")
            ui_file = QFile(path)
            ui_file.open(QFile.ReadOnly)
            loader.load(ui_file, self)
            ui_file.close()
    
    
    if __name__ == "__main__":
        app = QApplication([])
        widget = Widget()
        widget.show()
        sys.exit(app.exec_())
    
    
    

    What dis I made wrong ?

    E 1 Reply Last reply 9 Sept 2021, 14:36
    0
    • X Xav12358
      9 Sept 2021, 13:51

      Hello,

      I try to use QTDesign and integrate a image in a QLabel. So I create a .qrc and add my image:

      ac533fbe-a9c1-4aa2-8308-0bb27f75605a-image.png

      I the designer tool I see my image but not in my program:
      In designer tool
      6c6616b2-b3a6-46dd-a360-ab7e1b9b9b31-image.png

      I set the scalecontents. Here is the python code I use ( the default program)

      # This Python file uses the following encoding: utf-8
      import os
      from pathlib import Path
      import sys
      
      from PySide2.QtWidgets import QApplication, QWidget
      from PySide2.QtCore import QFile
      from PySide2.QtUiTools import QUiLoader
      
      
      class Widget(QWidget):
          def __init__(self):
              super(Widget, self).__init__()
              self.load_ui()
      
          def load_ui(self):
              loader = QUiLoader()
              path = os.fspath(Path(__file__).resolve().parent / "form.ui")
              ui_file = QFile(path)
              ui_file.open(QFile.ReadOnly)
              loader.load(ui_file, self)
              ui_file.close()
      
      
      if __name__ == "__main__":
          app = QApplication([])
          widget = Widget()
          widget.show()
          sys.exit(app.exec_())
      
      
      

      What dis I made wrong ?

      E Offline
      E Offline
      eyllanesc
      wrote on 9 Sept 2021, 14:36 last edited by eyllanesc 9 Sept 2021, 14:37
      #2

      @Xav12358 You must convert the .qrc to .py and then import it:

      rcc -g python assets.qrc -o assets_rc.py
      

      OR

      pyside2-rcc assets.qrc -o assets_rc.py
      

      THEN

      import os
      from pathlib import Path
      import sys
      
      from PySide2.QtWidgets import QApplication, QWidget
      from PySide2.QtCore import QFile
      from PySide2.QtUiTools import QUiLoader
      
      import assets_rc
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      3
      • X Offline
        X Offline
        Xav12358
        wrote on 10 Sept 2021, 08:40 last edited by
        #3

        Thank you it work properly.

        1 Reply Last reply
        0

        1/3

        9 Sept 2021, 13:51

        • 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