QMenuBar下面的QMDISubWindow

时间:2018-08-06 18:58:33

标签: python pyqt pyqt5

在我的PyQt程序中,我有一个QMenuBar启动QMDISubWindow小部件。默认情况下,它将在(0,0)附近启动。我对其进行了修改,以使其自动将小部件向下移动(如下所示)。

def check_position(self, y, width, height, sub_window):
    if y <= 40:
        sub_window.move(randint(0, self.width - width), randint(0, self.height - height))
        print("Sub Window Moved")

这会将QMDISubWindow向下移动,但是在全屏或向上拖动时,QMDISubWindow会(可能会)位于QMenuBar下方。您将如何解决该问题?

F.Y.I。完整的代码位于Github

1 个答案:

答案 0 :(得分:1)

尝试一下:

import sys
from random import randint

from PyQt5.QtWidgets import (QApplication, QMainWindow, QAction, QMenuBar, QWidget,
                             QMdiArea, QMdiSubWindow, QProxyStyle, QStyle, QVBoxLayout)
from PyQt5.QtGui     import (QIcon, QPainter, QPalette, QPixmap)
from PyQt5.QtCore    import Qt

from Browser_Tabbed import browser_tabbed
from Calculator     import calculator
from Notepad        import notepad
from Paint          import paint
from Solitaire      import solitaire


class MDIArea(QMdiArea):
    def __init__(self, *args, **kwargs):
        super(MDIArea, self).__init__(*args, **kwargs)  
        self.parent = args[0]
        self.background_pixmap = self.parent.pixmap 
        self.centered          = False
        self.display_pixmap    = None

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self.viewport())
        if not self.centered:
            painter.drawPixmap(0, 0, self.width(), self.height(), self.background_pixmap)
        else:
            painter.fillRect(event.rect(), self.palette().color(QPalette.Window))
            x = (self.width() - self.display_pixmap.width())/2
            y = (self.height() - self.display_pixmap.height())/2
            painter.drawPixmap(x, y, self.display_pixmap)
        painter.end()

    def resizeEvent(self, event):
        self.display_pixmap = self.background_pixmap.scaled(self.parent.window_width(), 
                                                            self.parent.window_height(), 
                                                            Qt.KeepAspectRatio)

class MyProxyStyle(QProxyStyle):
    def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None):
        if QStyle_PixelMetric == QStyle.PM_SmallIconSize:
            return 40
        else:
            return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)


class Desktop(QMainWindow):
    def __init__(self):
        super(Desktop, self).__init__()
        self.widthDesktop  = 900         # widthDesktop  +++
        self.heightDesktop = 650         # heightDesktop +++

        self.title = "Remote Desktop"
        self.left  = 200 
        self.top   = 70 

        self.initUI()
        self.create_mdi()                    # +++
        self.create_menu()                   # +++

        # +++
        self.centerWidget = QWidget(self)
        layout            = QVBoxLayout()  
        layout.addWidget(self.menu)
        layout.addWidget(self.mdi)
        self.centerWidget.setLayout(layout)
        self.setCentralWidget(self.centerWidget)

        self.show()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.widthDesktop, self.heightDesktop)

    def check_position(self, y, width, height, sub_window):
#        if y <= 40:
#            sub_window.move(randint(0, self.widthDesktop - width), 
#                            randint(0, self.heightDesktop - height) ) 
        if width < 500: 
            sub_window.move(randint(0, self.widthDesktop-(int(self.widthDesktop/2))), 
                            randint(0, self.heightDesktop-(int(self.heightDesktop/2))) ) 
        else:
            sub_window.move(randint(0, self.widthDesktop-(int(self.widthDesktop/1.5))), 
                            randint(0, self.heightDesktop-(int(self.heightDesktop/1.5))) )        

        print("Sub Window Moved")

    @staticmethod
    def close_desktop():
        print("Closing Remote Desktop")
        exit(0)

    def open_browser(self):
        print("Opening Browser")
        sub = QMdiSubWindow()
        sub.setWidget(browser_tabbed.MainWindow())
        sub.setWindowTitle("Browser")
        self.mdi.addSubWindow(sub)
        widget_position = sub.pos()
        widget_dimensions = sub.frameGeometry()
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=sub)
        sub.show()

    def open_calculator(self):
        print("Opening Calculator")
        sub = QMdiSubWindow()
        sub.setWidget(calculator.MainWindow())
        sub.setWindowTitle("Calculator")
        self.mdi.addSubWindow(sub)
        widget_position = sub.pos()
        widget_dimensions = sub.frameGeometry()
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=sub)
        sub.show()

    def open_notepad(self):
        print("Opening Notepad")
        sub = QMdiSubWindow()
        sub.setWidget(notepad.MainWindow())
        sub.setWindowTitle("Notepad")
        self.mdi.addSubWindow(sub)
        widget_position = sub.pos()
        widget_dimensions = sub.frameGeometry()
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=sub)
        sub.show()

    def open_paint(self):
        print("Opening Paint")
        sub = QMdiSubWindow()
        sub.setWidget(paint.MainWindow())
        sub.setWindowTitle("Paint")
        self.mdi.addSubWindow(sub)
        widget_position = sub.pos()
        widget_dimensions = sub.frameGeometry()
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=sub)
        sub.show()

    def open_solitaire(self):
        print("Opening Solitaire")
        sub = QMdiSubWindow()
        sub.setWidget(solitaire.MainWindow())
        sub.setWindowTitle("Solitaire")
        self.mdi.addSubWindow(sub)
        widget_position = sub.pos()
        widget_dimensions = sub.frameGeometry()
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=sub)
        sub.show()

    def create_menu(self):
        self.menu = QMenuBar(self)
        self.menu.setNativeMenuBar(False)

        exitButton = QAction(QIcon("Power.jpeg"), "Exit", self)
        exitButton.setShortcut("Ctrl+Q")
        exitButton.setStatusTip("Power Off")

        browser = QAction(QIcon("browser.jpeg"), "Boron", self)
        browser.setShortcut("Ctrl+B")
        browser.setStatusTip("Open Browser")

        calculator = QAction(QIcon("Calculator.jpeg"), "Cobalt", self)
        calculator.setShortcut("Ctrl+C")
        calculator.setStatusTip("Open Calculator")

        notepad = QAction(QIcon("Notepad.png"), "Neon", self)
        notepad.setShortcut("Ctrl+N")
        notepad.setStatusTip("Open Notepad")

        paint = QAction(QIcon("Paint.png"), "Paint", self)
        paint.setShortcut("Ctrl+P")
        paint.setStatusTip("Open Paint")

        solitaire = QAction(QIcon("Solitaire.jpeg"), "Xenon", self)
        solitaire.setShortcut("Ctrl+S")
        solitaire.setStatusTip("Open Solitaire")

        self.menu.addAction(exitButton)
        self.menu.addAction(browser)
        self.menu.addAction(calculator)
        self.menu.addAction(notepad)
        self.menu.addAction(paint)
        self.menu.addAction(solitaire)

        # +++
        self.menu.addAction("Cascade")
        self.menu.addAction("Tiled")
        self.menu.triggered[QAction].connect(self.windowaction)

        exitButton.triggered.connect(self.close_desktop)
        browser.triggered.connect(self.open_browser)
        calculator.triggered.connect(self.open_calculator)
        notepad.triggered.connect(self.open_notepad)
        paint.triggered.connect(self.open_paint)
        solitaire.triggered.connect(self.open_solitaire)

    # +++     
    def windowaction(self, q): 
        if q.text()=="Cascade":
            self.mdi.cascadeSubWindows()
        if q.text()=="Tiled":
            self.mdi.tileSubWindows()


    def window_width(self):
        self.widthDesktop = self.width()                     # +++
        return self.widthDesktop 

    def window_height(self):
        self.heightDesktop = self.height()                   # +++
        return self.heightDesktop 

    def create_mdi(self):
        self.pixmap = QPixmap()
        self.pixmap.load("mt-mckinley.jpg")
        #self.mdi = MDIArea(self.pixmap)     
        self.mdi = MDIArea(self)
        #self.setCentralWidget(self.mdi)
        #self.mdi.cascadeSubWindows()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex  = Desktop()
    #ex.create_mdi()
    #ex.create_menu()
    my_style = MyProxyStyle("Fusion")
    app.setStyle(my_style)
    app.exec_()

enter image description here