使用 Pyside6 时主窗口图标丢失

问题描述 投票:0回答:2

我可能遗漏了一些明显的东西,但下面的代码似乎没有在窗口标题栏中显示 app_icon。 app_icon.png 是一个 24x24px 文件,与 main.py 和 ui_mainwindow.py 位于同一文件夹中。 Qt docs似乎表明PySide6.QtGui.QIcon(fileName)是一种有效的方法,但也许我误解了它。除了这个问题之外,应用程序的其余部分工作正常。

from PySide6.QtGui import QIcon, QScreen
from PySide6.QtWidgets import QApplication, QFileDialog, QMainWindow
from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # Change a few things
        screenSize = QScreen.availableGeometry(QApplication.primaryScreen())
        winXpos = ((screenSize.width() - self.width())/2)
        winYpos = ((screenSize.height() - self.height())/2)
        self.move(winXpos, winYpos)
        self.setWindowTitle('App - Main Window')
        self.setWindowIcon(QIcon('app_icon.png'))
        ...rest of the code...

更改为以下内容,会导致错误

self.setWindowIcon(QIcon.addFile('app_icon.png'))
TypeError: descriptor 'addFile' for 'PySide6.QtGui.QIcon' objects doesn't apply to a 'str' object

这是在linux上,使用Python3.8.10和PySide6。该表单与 Qt Creator 组合在一起,并使用 pyside6-uic 将 .ui 转换为 .py。有什么想法我哪里出错了吗?

编辑: screenshot of missing icon, post changes suggested in the comments

python pyside6
2个回答
1
投票

我的猜测是,由于

addFile
不是静态函数,因此直接从
QIcon
类调用它是行不通的。相反,我认为您需要实例化一个
QIcon
对象并将
addFile
应用于它:

my_icon = QIcon()
my_icon.addFile('app_icon.png')

self.setWindowIcon(my_icon)

建议,测试该文件是否存在 (

os.path.isfile
) 以及在 Linux 上您是否具有对该文件的读取访问权限。或者,尝试从文件路径创建
QPixmap
并测试像素图是否为空(Python 中的
None

(正如 @musicamante 所建议的,可能由于某种原因找不到您的文件)。

另外,尝试一下

from PySide6.QtGui import QIcon, QPixmap

my_pixmap = QPixmap(":/app_icon.png")
my_icon = QIcon(my_pixmap)

self.setWindowIcon(my_icon)

有关详细信息,请参阅Qt 资源系统


0
投票

@adam.hendry:谢谢;您的解决方案非常完美。

# Image Sources
# URL: https://pngtree.com/freepng/eagle_716908.html (Eagle Head)

# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys

# Default MainWindow Project PySide6 Qt Application (Dynamic)
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader

# Import Required Modules for MainWindow Sizing
from PySide6.QtCore import QSize, Qt


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        # Set Main Window Title
        self.setWindowTitle("EXODUS 2030: American Christian Natural Born Allegiant - Mass Empowerments!")
        # Set Main Window Fixed Size (600 Height x 1000 Width Pixels)
        self.setFixedSize(QSize(1000,600 ))
        self.load_ui()

        # Import Required Modules for MainWindow Icon
        # Learning Source
        # URL: https://stackoverflow.com/questions/70228802/main-window-icon-missing-using-pyside6

        from PySide6.QtGui import QIcon, QPixmap

        my_pixmap = QPixmap("images/Eagle_Head_30x30.png")
        my_icon = QIcon(my_pixmap)

        self.setWindowIcon(my_icon)



    def load_ui(self):
        loader = QUiLoader()
        path = Path(__file__).resolve().parent / "form.ui"
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = MainWindow()
    widget.show()
    sys.exit(app.exec())

效果很好! Qt Creator 6 [9.0.2] 上的 debian-12.5.0-amd64 sddm/lxqt 中的图标基于 Qt 6.4.2 (GCC 12.2.0, x86_64),使用 PySide6 和主应用程序/窗口(动态)w/ Python 3.12 .3(从源代码编译)|任务栏和主窗口的左上角!

非常感谢,先生!

  • 布兰登·卡斯宁(DNinja)
© www.soinside.com 2019 - 2024. All rights reserved.