在QMainWindow最小化时使模态QDialog最小化(使用PyQt 5)

时间:2016-12-26 11:45:44

标签: python python-3.x pyqt pyqt5

我正在使用的示例代码:

import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.open_about = False
        self.openAction = QtWidgets.QAction('About', self)
        self.openAction.triggered.connect(self.aboutDialog)
        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu('&File')
        fileMenu.addAction(self.openAction)
        self.calendar = QtWidgets.QCalendarWidget(self)
        self.setCentralWidget(self.calendar)

    def about_state_upd(self, value):
        self.open_about = value

    def aboutDialog(self):
        self._about = AboutDialog(self)
        self._about.exec_()

    def hideEvent(self, hideEvent):
        if self.open_about == True:
            self._about.setVisible(False)

    def showEvent(self, showEvent):
        if self.open_about == True:
            if self._about.isHidden() == True:
                self._about.setModal(True)
                self._about.setVisible(True)

class AboutDialog(QtWidgets.QDialog):

    def __init__(self, parent):
        super(AboutDialog, self).__init__(parent)

        self.setMinimumSize(400, 350)
        self.parent().about_state_upd(True)

    def closeEvent(self, closeEvent):
        self.parent().about_state_upd(False)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app_window = MainWindow()
    app_window.showMaximized()
    sys.exit(app.exec_())

这段代码基本上有效,但看起来很复杂。是否有更简单/更清晰的方法使得当模态QDialog打开时,如果QMainWindow最小化,QDialog也会被最小化(当QMainWindow恢复时反向)?

代码正在KDE Neon(基于Kubuntu的发行版)上运行。

2 个答案:

答案 0 :(得分:1)

可能你可以使用这个:http://korbinin.blogspot.fr/search/label/minimize%20button



package main

import (
    "fmt"
    "log"
    "time"
)

const layout = "2006-01-02"

func main() {
    snapshot := "2017-07-25"
    t, err := time.Parse(layout, snapshot)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(t)
}




答案 1 :(得分:0)

感谢PyQt邮件列表中的人员,我设法为KDE找到了解决方法。而不是使用exec_(),我只是使用show() - 然后我在QMainWindow上使用setDisabled()来使对话框以模态方式运行。对于任何感兴趣的人来说,这是一个(非常快速和基本的)示例:

import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.openAction = QtWidgets.QAction('About', self)
        self.openAction.triggered.connect(self.aboutDialog)
        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu('&File')
        fileMenu.addAction(self.openAction)
        self.calendar = QtWidgets.QCalendarWidget(self)
        self.setCentralWidget(self.calendar)

    def aboutDialog(self):
        self._about = AboutDialog(self)
        self.setDisabled (True)
        self._about.show()

    def enableWidgets(self):
        self.setDisabled(False)

class AboutDialog(QtWidgets.QDialog):

    def __init__(self, parent):
        super(AboutDialog, self).__init__(parent)

        self.setMinimumSize(400, 350)

    def closeEvent(self, parent):
        self.parent().enableWidgets()

    def changeEvent(self, event):
        if event.type() == QtCore.QEvent.WindowStateChange:
            if self.windowState() & QtCore.Qt.WindowMinimized:
                self.parent().showMinimized()
            else:
                self.parent().showMaximized()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app_window = MainWindow()
    app_window.showMaximized()
    sys.exit(app.exec_())

链接到PyQt Mailing List posts