有没有一种方法可以防止在父窗口上调用hide时隐藏任务栏图标?

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

我有一个父子窗口。当您按下父类的按钮时,将显示子代,而父代则被隐藏。无论如何,是否可以使任务栏图标保持可见?

from PyQt5.QtCore import Qt, QDateTime
from PyQt5.QtWidgets import *
from PyQt5 import QtGui

class ParentWindow(QDialog):
    def __init__(self):
        super(ParentWindow, self).__init__()
        self.cw = None
        self.button = QPushButton('Go to child')
        self.button.clicked.connect(self.child)
        layout = QHBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)

        self.show()

    def child(self):
        self.cw = ChildWindow(self)
        self.hide()

    def parent(self):
        self.cw.close()
        self.show()

class ChildWindow(QDialog):
    def __init__(self, parent):
        super(ChildWindow, self).__init__(parent)
        self.button = QPushButton('Go to parent')
        self.button.clicked.connect(self.parent().parent)
        layout = QHBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.show()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = ParentWindow()
    sys.exit(app.exec_())
python pyqt pyqt5
1个回答
0
投票

问题是子级QDialog与父级“共享”相同的任务栏条目,而父级被隐藏时,无论是否可见子级,任务栏条目也都共享。

一种可能的解决方案是不隐藏父级,而将其最小化。请记住,这仅在Windows上可以进行测试,在Linux上,您可能会有意外的行为(取决于窗口管理器与模式窗口的关系),我不知道它如何在MacO上运行。

想法是在父级上使用showMinimized(),然后向子级显示activate,以使其在顶部可见,最重要的是,使用exec_()以确保其具有完全控制权并使其表现为对话框应该。然后,我们将对话框的接受信号和拒绝信号都连接起来,以恢复主窗口,然后将单击的信号连接起来以接受。

class ParentWindow(QDialog):
    def __init__(self):
        super(ParentWindow, self).__init__()
        self.cw = None
        self.button = QPushButton('Go to child')
        self.button.clicked.connect(self.child)
        layout = QHBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)

        self.show()

    def child(self):
        self.cw = ChildWindow(self)
        self.showMinimized()
        self.cw.show()
        self.cw.activateWindow()
        self.cw.exec_()

class ChildWindow(QDialog):
    def __init__(self, parent):
        super(ChildWindow, self).__init__(parent)
        self.button = QPushButton('Go to parent')
        self.button.clicked.connect(self.accept)
        layout = QHBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.accepted.connect(self.parent().showNormal)
        self.rejected.connect(self.parent().showNormal)

PS:我最终根本没有使用自定义函数来回调父级,但是请记住,您不应覆盖诸如parent之类的函数/属性名称。

© www.soinside.com 2019 - 2024. All rights reserved.