项目结构设计:在哪里存储工具栏图标以及如何访问它们

问题描述

我正在开发一个包含一组图标图像的 GUI 项目。我本能地在项目根目录中创建了一个 img 目录。

我计划使用应用程序的根类作为存储此类内容的地方,并在字典中收集 QIcon 对象

self.toolbarIcons = {QIcon("<path-to-image.png>")}

那么问题是如何从层次结构中的几个类中最好地访问这些图标。当我使用 tkinter 时,结构非常线性,因为每个小部件都是其父级的子级。

我设置 Qt 应用程序的方式(使用 PySide6),基类是 QApplication。在这里,我构建了一个 QMainWindow,我在其中设置了各种小部件(中央小部件、工具栏、状态栏等)。

随着应用程序复杂性的增加,有什么好的策略可以很好地扩展?我是否应该将与特定小部件相关的图标存储为该特定类的类属性(从而在整个代码中散布图标对象)?我喜欢将图标对象放在一起的想法。

我把代码分在不同的目录下,但基本上结构是这样的(MWE):

from PySide6.QtWidgets import QApplication,QMainWindow,QTableWidget,QToolBar
from PySide6.QtGui import QIcon


class MyApplication(QApplication):
    def __init__(self,*args,**kwargs):
        QApplication.__init__(self,**kwargs)
        
        self.icons = {'do_stuff': QIcon('<path-to-icon.png>')}
        self.mainwindow = MyMainWindow()
        self.mainwindow.show()


class MyMainWindow(QMainWindow):
    def __init__(self,**kwargs):
        QMainWindow.__init__(self,**kwargs)

        self.setCentralWidget(MyCentralWidget(self))
        self.addToolBar(MyToolBar(self))


class MyCentralWidget(QTableWidget):
    def __init__(self,parent,**kwargs):
        QTableWidget.__init__(self,**kwargs)
        self.parent = parent


class MyToolBar(QToolBar):
    def __init__(self,**kwargs):
        QToolBar.__init__(self,**kwargs)
        self.parent = parent

        # How to access MyApplication.icons['do_stuff'] from here?


if __name__ == '__main__':
    app = MyApplication()
    app.exec_()


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)