loadUi()在PyQt中加载除了没有图像的所有东西

时间:2018-05-31 14:55:50

标签: qt pyqt

我在Qt Designers中创建了我的.ui文件然后使用loadUI()将其加载到我的应用程序中。

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_())

然而,当我去运行时,图像将不会显示,这在Qt设计器中是奇怪的,当我点击预览图像实际显示时。

另外,如果它有用,我会使用xz.prc文件作为前缀/路径,这就是Qt设计师编写链接到图像的代码的方式。

请问您可以在我的应用中建议我从new.ui节目中获取图片的方式吗?此外,所有小部件都可以工作,包括标签,它只是我运行时不显示的图像。

由于 卡尔

.ui文件采用原生格式

<?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 这是在从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()

1 个答案:

答案 0 :(得分:0)

.qrc文件可能是Qt设计器用来连接图像的,但是当我将.ui文件加载到我的.py应用程序时,它在使用.qrc文件时遇到了麻烦,所以我需要创建一个.py版本的.qrc。

使用命令提示符转到.qrc所在的目录并写入此命令。注意xz就是我所说的你可能被称为其他东西。

pyrcc5 xz.qrc -o xz_rc.py

然后将其导入.py应用程序,无需在末尾添加.py,因为我们将其作为模块导入。

import xz.rc

现在你的.py应用程序将显示图像,因为它可以访问现在可以理解的格式。