Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
loadUi()加载PyQt中的所有内容,但不加载任何图像_Qt_Pyqt - Fatal编程技术网

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

loadUi()加载PyQt中的所有内容,但不加载任何图像,qt,pyqt,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(Life2

我已经在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 show获取图像的方式好吗?此外,所有的小部件都可以工作,包括标签,只是运行此程序时不会显示的图像

谢谢 卡尔

.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>

对话
0
0
1343
965
对话
730
740
521
171
QFrame::Box
:/newPrefix/me.jpg
60
530
651
271
:/newPrefix/logo.png
930
70
331
301
图像:url(:/newPrefix/logo.png);
文本标签
280
180
68
19
文本标签
xz.prc

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

logo.png
me.jpg
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()
从PyQt5导入QtCore
qt_资源_数据=b
qt_version=QtCore.qVersion().split('.'))
如果qt_版本<['5','8','0']:
rcc_版本=1
qt\u资源结构=qt\u资源结构v1
其他:
rcc_版本=2
qt\u资源结构=qt\u资源结构v2
def qInitResources():
QtCore.qRegisterResourceData(rcc_版本、qt_资源结构、qt_资源名称、qt_资源数据)
def qCleanupResources():
QtCore.qnRegisterResourceData(rcc_版本、qt_资源结构、qt_资源名称、qt_资源数据)
qInitResources()

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

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

pyrcc5 xz.qrc -o xz_rc.py
然后将其导入到您的.py应用程序中,无需在末尾添加.py,因为我们将其作为模块导入

import xz.rc

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

你可以提供.ui。不要将其转换为.py,只需显示.ui,.ui是一个.xml,所以你应该可以用任何合适的编辑器打开它:)哦,对不起,我刚刚删除了.py转换的部分,只保留了原生的.ui。谢谢你试过把.qrc转换成.py吗
pyrcc5 xz.qrc-o xz_rc.py
我刚刚尝试了一下,我创建了.py,我运行了应用程序,但它仍然没有显示图像。我需要更改任何其他内容才能使用xz_rc.py吗?