MacOS PyQt5 - Right-click 停靠栏中的图标显示所有打开的 windows

MacOS PyQt5 - Right-click icon in dock to show all open windows

标题几乎说明了一切。

如何让使用 PyQt5 构建的应用程序具有类似于 window 的行为,如下所示?理想情况下,所有打开的 windows 都将与前景中的 window 指示器一起显示。

FIJI 应用程序图标 Right-Click:

PyQt5 应用程序图标 Right-Click(打开多个 windows):

好的 - 这是我开发的粗略解决方案。我仍然需要弄清楚如何在 window 项目被销毁后从停靠栏中删除它们,我想添加指示器来显示 windows 是可见还是隐藏。这是一个好的开始!

感谢@musicamante 的 setAsDockMenu() 标志。

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow,QWidget, QPushButton,
                             QMenu, QVBoxLayout, QAction)

import sys
import platform
    
def add_menu(mainWindow):
    if platform.system() == "Darwin":
        mainWindow.menuBar = MenuBar()
        mainWindow.centralWidget().layout().addWidget(mainWindow.menuBar)


class MenuBar(QMenu):
    """ Menu bar that displays the open windows on MacOS """
    menu_items = {}
    def __init__(self):
        super().__init__()
        self.setAsDockMenu()
        self.hide()
    
    def add_window(self, window, title):
        show_action = self.addAction(title)
        show.triggered.connect(window.showNormal)
        self.menu_items[title] = show_action
        return
        
    def remove_window(self, title):
        show_action = self.menu_items.pop[title]
        self.removeAction(show_action)
        return

class Window(QWidget):
    def __init__(self, title, windows, dockMenu):
        super().__init__()
        
        self.title = title
        self.windows = windows
        self.dockMenu = dockMenu
        self.setWindowTitle(title)
        self.show()
        
    def closeEvent(self, event):
        self.windows.pop(self.title)
        
        if platform.system() == "Darwin":
            self.dockMenu.remove_window(self.title)
            
        event.accept()
        self.destroy()


class MainWindow(QMainWindow):
    windows = {}
    def __init__(self):
        super().__init__()
        
        self.setCentralWidget(QWidget())
        c_widget = self.centralWidget()
        c_widget.setLayout(QVBoxLayout())
        
        # If using MacOS, create a menubar to view windows.
        add_menu(self)
        
        add_item = QPushButton("Add")
        add_item.clicked.connect(self.add_window)
        c_widget.layout().addWidget(add_item)
        
        print_window = QPushButton("Print Windows")
        print_window.clicked.connect(self.show_windows)
        c_widget.layout().addWidget(print_window)
        
        self.show()
        
    def add_window(self):
        title = str(len(self.windows.keys()))
        self.window = Window(title, self.windows, self.menuBar)
        self.windows[title] = self.window
        
        self.menuBar.add_window(self.window, title)
        
    def show_windows(self):
        print (self.windows.keys())
    
    def closeEvent(self, event):
        QApplication.closeAllWindows()
        event.accept()
        
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())