首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >loadUi()加载PyQt中的所有内容,但不加载图像

loadUi()加载PyQt中的所有内容,但不加载图像
EN

Stack Overflow用户
提问于 2018-05-31 22:55:50
回答 1查看 1.1K关注 0票数 0

我已经在Qt设计器中创建了我的.ui文件,然后使用loadUI()将其加载到下面的应用程序中。

代码语言:javascript
复制
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 show中获取图像的方法吗?此外,所有的窗口小部件的工作,包括标签,它只是图像不显示时,我运行这个。

谢谢你,卡尔

本机格式为的.ui文件

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<RCC>
  <qresource prefix="newPrefix">
    <file>logo.png</file>
    <file>me.jpg</file>
  </qresource>
</RCC>

xz_rc.py这是在它从xz.prc转换之后

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-01 03:18:15

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

使用命令提示符转到.qrc所在的目录,然后输入以下命令。注意: xz就是我所说的,你也可以称之为其他的东西。

代码语言:javascript
复制
pyrcc5 xz.qrc -o xz_rc.py

然后将其导入到您的.py应用程序中,不需要在末尾添加.py,因为我们将其作为模块导入。

代码语言:javascript
复制
import xz.rc

现在,您的.py应用程序应该可以显示图像,这要归功于它可以访问它现在可以理解的格式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50627220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档