gpt4 book ai didi

python - 图形在 Qt Designer 中显示,但在 PyQt5 GUI 中不显示

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:31 26 4
gpt4 key购买 nike

我想在 Qt Designer 创建的 mainwindow.ui 文件中的 QLabel 中显示 SVG 图形。

SVG 图形显示在 Qt Designer 中,但不使用下面的 Python 3 代码。

代码有什么问题?

主.py

import sys
from pathlib import Path
from PyQt5.QtWidgets import QApplication, QDesktopWidget, QMainWindow
from PyQt5 import uic


MainWindow_form = (Path(__file__).parent / "ui/mainwindow.ui").resolve()
MainWindow_Interface, _ = uic.loadUiType(MainWindow_form)


class MainWindow(QMainWindow, MainWindow_Interface, QDesktopWidget):
def __init__(self, parent=None):
super().__init__()
self.setupUi(self)


def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())


if __name__ == "__main__":
main()

ui/mainwindow.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>516</width>
<height>606</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="locale">
<locale language="English" country="UnitedKingdom"/>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_graphics">
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap>../svg/creative.svg</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_attribution">
<property name="text">
<string>Icon made by Eucalyp from www.flaticon.com</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>516</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
</widget>
<resources/>
<connections/>
</ui>

svg/creative.svg

<svg height="496pt" viewBox="0 0 496 496" width="496pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><linearGradient id="a" gradientUnits="userSpaceOnUse" x1="248" x2="248" y1="496" y2="0"><stop offset="0" stop-color="#9f2fff"/><stop offset="1" stop-color="#0bb1d3"/></linearGradient><path d="m224 48h-192v-16h192zm-16 32h16v-16h-16zm-16-16h-160v16h160zm32 32h-192v16h192zm-201.480469 316.175781-22.519531-6.039062v-60.273438l22.519531-6.039062c.894531-2.382813 1.886719-4.761719 2.992188-7.136719l-11.671875-20.230469 42.609375-42.625 20.230469 11.671875c2.367187-1.097656 4.753906-2.097656 7.136718-2.992187l4.183594-15.574219v-109.546875l-10.960938-9.390625h-53.039062c-13.230469 0-24-10.769531-24-24v-96c0-13.230469 10.769531-24 24-24h208c13.230469 0 24 10.769531 24 24v40h150.671875l33.328125-55.550781 56 93.335937v354.214844c0 22.054688-17.945312 40-40 40h-32c-22.054688 0-40-17.945312-40-40v-8h-166.296875l-34.152344 34.167969-20.230469-11.671875c-2.367187 1.097656-4.753906 2.097656-7.136718 2.992187l-6.039063 22.511719h-60.28125l-6.039062-22.519531c-2.382813-.894531-4.769531-1.886719-7.136719-2.992188l-20.230469 11.671875-42.617187-42.617187 11.671875-20.230469c-1.097657-2.367188-2.085938-4.753906-2.992188-7.136719zm377.480469-28.175781h32v-32h16v32h32v-272h-32v224h-16v-224h-32zm0 16v16h80v-16zm-16-128h-112v128h112zm96 184v-24h-80v24c0 13.230469 10.769531 24 24 24h32c13.230469 0 24-10.769531 24-24zm-40-416.449219-33.871094 56.449219h67.742188zm-357.039062 88.449219 45.039062 38.609375v-38.609375h104c4.414062 0 8-3.59375 8-8v-96c0-4.40625-3.585938-8-8-8h-208c-4.414062 0-8 3.59375-8 8v96c0 4.40625 3.585938 8 8 8zm21.039062 39.113281v88.886719h46.144531l6.039063 22.519531c2.382812.894531 4.769531 1.886719 7.136718 2.992188l20.230469-11.671875 42.609375 42.625-11.671875 20.230468c1.105469 2.367188 2.097657 4.753907 2.992188 7.136719l22.519531 6.03125v60.273438l-22.519531 6.039062c-.894531 2.382813-1.886719 4.761719-2.992188 7.136719l7.320313 12.6875h162.191406v-16h-128v-160h128v-154.214844l13.070312-21.785156h-141.070312v40c0 13.230469-10.769531 24-24 24h-88v57.390625zm-88 226.75 18.945312 5.082031 1.34375 4.132813c1.390626 4.289063 3.199219 8.632813 5.398438 12.914063l1.976562 3.871093-9.832031 17.046875 25.265625 25.265625 17.046875-9.839843 3.871094 1.984374c4.273437 2.183594 8.617187 4 12.90625 5.390626l4.132813 1.335937 5.089843 18.953125h35.71875l5.089844-18.953125 4.136719-1.335937c4.285156-1.390626 8.628906-3.207032 12.902344-5.390626l3.871093-1.984374 17.046875 9.839843 25.265625-25.265625-9.832031-17.046875 1.976562-3.871093c2.191407-4.289063 4.007813-8.632813 5.398438-12.914063l1.34375-4.132813 18.9375-5.082031v-35.726562l-18.945312-5.082031-1.34375-4.132813c-1.390626-4.289063-3.199219-8.632813-5.398438-12.914063l-1.976562-3.871093 9.832031-17.046875-25.265625-25.265625-17.046875 9.839843-3.871094-1.984374c-4.273437-2.183594-8.617187-4-12.90625-5.390626l-4.132813-1.335937-5.089843-18.953125h-35.71875l-5.089844 18.953125-4.136719 1.335937c-4.285156 1.390626-8.628906 3.207032-12.902344 5.390626l-3.871093 1.984374-17.046875-9.839843-25.265625 25.265625 9.832031 17.046875-1.976562 3.871093c-2.191407 4.289063-4.007813 8.632813-5.398438 12.914063l-1.34375 4.132813-18.9375 5.082031zm32-17.863281c0-39.695312 32.296875-72 72-72s72 32.304688 72 72-32.296875 72-72 72-72-32.304688-72-72zm16 0c0 30.878906 25.128906 56 56 56s56-25.121094 56-56-25.128906-56-56-56-56 25.121094-56 56zm16 0c0-22.054688 17.945312-40 40-40s40 17.945312 40 40-17.945312 40-40 40-40-17.945312-40-40zm16 0c0 13.230469 10.769531 24 24 24s24-10.769531 24-24-10.769531-24-24-24-24 10.769531-24 24zm272-280h-96v16h96zm0 32h-96v16h96zm0 32h-208v16h208zm0 32h-208v16h208zm0 48v-16h-248v16zm0 0" fill="url(#a)"/></svg>

最佳答案

loadUiType()创建一个类作为 main.py 文件路径的基础,这与 Qt Designer 不同,Qt Designer 用作 .ui 路径的基础,导致目录之间出现问题。于是就有了以下解决方案:

  • 修改 .ui 中 .svg 的路径,考虑到文件的基础是 .py,为此它更改为:
<pixmap>svg/creative.svg</pixmap>
  • 使用代码设置 svg:
self.setupUi(self)
<b>svg_path = (Path(__file__).parent / "svg/creative.svg").resolve()
self.label_graphics.setPixmap(QtGui.QPixmap(str(svg_path)))</b>

关于python - 图形在 Qt Designer 中显示,但在 PyQt5 GUI 中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58148793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com