Carl
Carl

Reputation: 71

loadUi() loads everything but no Images in PyQt

I have created my .ui file in Qt Designers I am then using loadUI() to load it into my app below.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.uic import loadUi


class Life2Coding(QDialog):
    def __init__(self):
        super(Life2Coding, self).__init__()
        loadUi('new.ui', self)

app = QApplication(sys.argv)
widget=Life2Coding()
widget.show()
sys.exit(app.exec_())

However when I go to run this the images will not show up, which is odd as in the Qt designer when I click preview the images actually show up.

Also on a side note if its helpful I am using a xz.prc file for a prefix / path which is how the Qt designer is writing the code linking to the images.

Please could you suggest ways in which I might get the images from new.ui show in my app? Also all widgets work including labels its just the images that do not show up when I run this.

Thanks Carl

.ui file in its native format

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
  <rect>
<x>0</x>
<y>0</y>
<width>1343</width>
<height>965</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
  <rect>
 <x>730</x>
 <y>740</y>
 <width>521</width>
 <height>171</height>
</rect>
</property>
<property name="frameShape">
    <enum>QFrame::Box</enum>
   </property>
   <property name="text">
    <string/>
    </property>
   <property name="pixmap">
    <pixmap resource="xz.qrc">:/newPrefix/me.jpg</pixmap>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>530</y>
     <width>651</width>
     <height>271</height>
     </rect>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="pixmap">
    <pixmap resource="xz.qrc">:/newPrefix/logo.png</pixmap>
   </property>
 </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>930</x>
     <y>70</y>
     <width>331</width>
     <height>301</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">image: url(:/newPrefix/logo.png);</string>
   </property>
   <property name="text">
    <string>TextLabel</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_4">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>180</y>
     <width>68</width>
    <height>19</height>
    </rect>
   </property>
   <property name="text">
    <string>TextLabel</string>
   </property>
  </widget>
 </widget>
 <resources>
  <include location="xz.qrc"/>
 </resources>
 <connections/>
</ui>

xz.prc

<RCC>
  <qresource prefix="newPrefix">
    <file>logo.png</file>
    <file>me.jpg</file>
  </qresource>
</RCC>

xz_rc.py This is after it has been converted from xz.prc

from PyQt5 import QtCore

qt_resource_data = b

 qt_version = QtCore.qVersion().split('.')
    if qt_version < ['5', '8', '0']:
        rcc_version = 1
        qt_resource_struct = qt_resource_struct_v1
    else:
        rcc_version = 2
        qt_resource_struct = qt_resource_struct_v2

    def qInitResources():
        QtCore.qRegisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)

    def qCleanupResources():
        QtCore.qUnregisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)

    qInitResources()

Upvotes: 0

Views: 2032

Answers (1)

Carl
Carl

Reputation: 71

The .qrc file maybe what Qt designer uses to connect to images however when I am loading the .ui file into my .py app it is having trouble using the .qrc file so instead I need to create a .py version of .qrc.

Go to the directory that .qrc is located in using command prompt and write in this command. Note xz is just what I called it yours may be called thing else.

pyrcc5 xz.qrc -o xz_rc.py

Then import this into your .py app no need to add .py on the end as we are importing this as a module.

import xz.rc

Now your .py app shall display images thanks to having access to a format it can now understand.

Upvotes: 3

Related Questions