我有一个带有以下 main.py 代码的基本 Pyside2 项目:
import sys
import resources_rc
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
class MyApp():
def __init__(self):
super().__init__()
self.ui = QUiLoader().load("MyApp.ui")
self.ui.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MyApp()
app.exec_()
我的文件夹结构如下所示:
.
├── main.py
├── MyApp.ui
├── Resources.qrc
├── resources_rc.py
└── icons
├── pqClip.png
├── pqSlice.png
├── pqThreshold.png
├── pqXplus.png
├── pqXminus.png
├── pqYplus.png
├── pqYminus.png
├── pqZplus.png
├── pqZminus.png
我的 Resources.qrc 文件如下所示:
<RCC>
<qresource>
<file>icons/pqClip.png</file>
<file>icons/pqSlice.png</file>
<file>icons/pqThreshold.png</file>
<file>icons/pqXplus.png</file>
<file>icons/pqXminus.png</file>
<file>icons/pqYplus.png</file>
<file>icons/pqYminus.png</file>
<file>icons/pqZplus.png</file>
<file>icons/pqZminus.png</file>
</qresource>
</RCC>
我的应用程序.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1217</width>
<height>729</height>
</rect>
</property>
<property name="windowTitle">
<string>MyApp</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="horizontalSpacing">
<number>4</number>
</property>
<item row="0" column="0">
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QFrame" name="frame_2">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="verticalSpacing">
<number>2</number>
</property>
<item row="0" column="0">
<widget class="QWidget" name="menuWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="menuLayout">
<property name="spacing">
<number>4</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="filterBtnWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="filterBtnLayout">
<property name="spacing">
<number>4</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="clipBtn">
<property name="toolTip">
<string>Add Clip Filter</string>
</property>
<property name="statusTip">
<string/>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqClip.png</normaloff>:/icons/pqClip.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="sliceBtn">
<property name="toolTip">
<string>Add Slice Filter</string>
</property>
<property name="statusTip">
<string/>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqSlice.png</normaloff>:/icons/pqSlice.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="thresholdBtn">
<property name="toolTip">
<string>Add Threshold Filter</string>
</property>
<property name="statusTip">
<string/>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqThreshold.png</normaloff>:/icons/pqThreshold.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>1</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QWidget" name="cameraAxisWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="cameraAxisLayout">
<property name="spacing">
<number>4</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="cameraXpBtn">
<property name="toolTip">
<string extracomment="X+">Set view direction to +X</string>
</property>
<property name="statusTip">
<string>Set view direction to +X</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqXplus.png</normaloff>:/icons/pqXplus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cameraXmBtn">
<property name="toolTip">
<string extracomment="X-">Set view direction to -X</string>
</property>
<property name="statusTip">
<string>Set view direction to -X</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqXminus.png</normaloff>:/icons/pqXminus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cameraYpBtn">
<property name="toolTip">
<string extracomment="Y+">Set view direction to +Y</string>
</property>
<property name="statusTip">
<string>Set view direction to +Y</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqYplus.png</normaloff>:/icons/pqYplus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cameraYmBtn">
<property name="toolTip">
<string extracomment="Y-">Set view direction to -Y</string>
</property>
<property name="statusTip">
<string>Set view direction to -Y</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqYminus.png</normaloff>:/icons/pqYminus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cameraZpBtn">
<property name="toolTip">
<string extracomment="Z+">Set view direction to +Z</string>
</property>
<property name="statusTip">
<string>Set view direction to +Z</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqZplus.png</normaloff>:/icons/pqZplus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cameraZmBtn">
<property name="toolTip">
<string extracomment="Z-">Set view direction to -Z</string>
</property>
<property name="statusTip">
<string>Set view direction to -Z</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Workspace/MyApp/Resources.qrc">
<normaloff>:/icons/pqZminus.png</normaloff>:/icons/pqZminus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<action name="actionRegistration">
<property name="text">
<string>Registration</string>
</property>
</action>
<action name="actionI_O">
<property name="text">
<string>I/O</string>
</property>
</action>
<action name="actionFusion">
<property name="text">
<string>Fusion</string>
</property>
</action>
<action name="actionCustom">
<property name="text">
<string>Custom</string>
</property>
</action>
</widget>
<resources>
<include location="../Workspace/MyApp/Resources.qrc"/>
</resources>
<connections/>
</ui>
我使用 Qt Designer 在我的用户界面中的各种按钮上设置了 Resources.qrc 文件中的图标。
我运行了这个命令来创建我的 resources_rc.py 文件:pyside2-rcc -o resources_rc.py Resources.qrc
.
当我运行python main.py
以执行我的应用程序时,我的 QRC 文件中的任何图标都不会出现在用户界面中。
我还应该做些什么来让它发挥作用吗?谢谢。