Qmdiarea中的中心子窗口

时间:2019-04-27 01:22:17

标签: python python-3.x pyqt pyqt5 qmdiarea

在qmdiarea中是否有放置子窗口的属性?我正在尝试在启动(mdiarea)时将子窗口居中在主窗口中间

我正在研究mcve,但尚未完成,想看看是否有人尝试过这样做,以及他们是如何做到的

子窗口在初始化时会随机放置在启动状态

class App(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent=parent)
        self.setupUi(self)
        self.screenShape = QDesktopWidget().screenGeometry()
        self.width = self.screenShape.width()
        self.height = self.screenShape.height()
        self.resize(self.width * .6, self.height * .6)
        self.new = []
#calls GUI's in other modules
        self.lw = Login()
        self.vs = VS()
        self.ms = MS()
        self.hw = HomeWindow()
        self.mw = MainWindow()
        self.ga = GA()
        self.sGUI = Settings()
# shows subwindow
        self.CreateLogin()
        self.CreateVS()
        self.CreateMS()
        self.CreateGA()
        self.CreateSettings()

    def CreateLogin(self):
        self.subwindow = QMdiSubWindow()
        self.subwindow.setWidget(self.lw)
        self.subwindow.setAttribute(Qt.WA_DeleteOnClose, True)
        self.mdiArea.addSubWindow(self.subwindow)
        self.subwindow.setMaximumSize(520, 300)
        self.subwindow.setMinimumSize(520, 300)
        self.lw.showNormal()

    def CreateVS(self):
        self.subwindow = QMdiSubWindow()
        self.subwindow.setWidget(self.vs)
        self.mdiArea.addSubWindow(self.subwindow)
        self.vs.showMinimized()

    def CreateMS(self):
        self.subwindow = QMdiSubWindow()
        self.subwindow.setWidget(self.ms)
        self.mdiArea.addSubWindow(self.subwindow)
        self.ms.showMinimized()
        self.ms.tabWidget.setCurrentIndex(0)

    def CreateGA(self):
        self.subwindow = QMdiSubWindow()
        self.subwindow.setWidget(self.ga)
        self.mdiArea.addSubWindow(self.subwindow)
        self.ga.showMinimized()
        self.subwindow.setMaximumSize(820, 650)

    def CreateSettings(self):
        self.subwindow = QMdiSubWindow()
        self.subwindow.setWidget(self.sGUI)
        self.mdiArea.addSubWindow(self.subwindow)
        self.sGUI.showMinimized()

    def CreateWindow(self):
        self.hw.pushButton.clicked.connect(self.vs.showNormal)
        self.hw.pushButton_2.clicked.connect(self.Moduleprogram)
        self.hw.pushButton_3.clicked.connect(self.ms.showNormal)
        self.hw.pushButton_4.clicked.connect(self.ga.showNormal)
        self.subwindow = QMdiSubWindow()
        self.subwindow.setWindowFlags(Qt.CustomizeWindowHint | Qt.Tool)
        self.subwindow.setWidget(self.hw)
        self.subwindow.setMaximumSize(258, 264)
        self.subwindow.move(self.newwidth*.35, self.newheight*.25)
        self.mdiArea.addSubWindow(self.subwindow)

1 个答案:

答案 0 :(得分:2)

在Qt中,几何仅在窗口可见时才有效,因此如果要居中放置某些东西,必须在showEvent方法中。另一方面,要使QMdiSubWindow居中,必须首先获取QMdiArea视口的中心,然后根据该视口修改QMdiSubWindow的几何形状。

由于您提供的代码执行起来很复杂,所以我创建了自己的代码

from PyQt5 import QtCore, QtGui, QtWidgets
import random


def create_widget():
    widget = QtWidgets.QLabel(
        str(random.randint(0, 100)), alignment=QtCore.Qt.AlignCenter
    )
    widget.setStyleSheet(
        """background-color: {};""".format(
            QtGui.QColor(*random.sample(range(255), 3)).name()
        )
    )
    widget.setMinimumSize(*random.sample(range(100, 300), 2))
    return widget


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        add_button = QtWidgets.QPushButton(
            "Add subwindow", clicked=self.add_subwindow
        )
        self._mdiarea = QtWidgets.QMdiArea()

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(add_button)
        lay.addWidget(self._mdiarea)

        self._is_first_time = True

        for _ in range(4):
            self.add_subwindow()

    @QtCore.pyqtSlot()
    def add_subwindow(self):
        widget = create_widget()
        subwindow = QtWidgets.QMdiSubWindow(self._mdiarea)
        subwindow.setWidget(widget)
        subwindow.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
        subwindow.show()
        self._mdiarea.addSubWindow(subwindow)
        # self.center_subwindow(subwindow)

    def showEvent(self, event):
        if self.isVisible() and self._is_first_time:
            for subwindow in self._mdiarea.subWindowList():
                self.center_subwindow(subwindow)
            self._is_first_time = False

    def center_subwindow(self, subwindow):
        center = self._mdiarea.viewport().rect().center()
        geo = subwindow.geometry()
        geo.moveCenter(center)
        subwindow.setGeometry(geo)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

更新

如果要使子窗口居中,则必须使用以下代码将属性中心创建为True:

def add_subwindow(self):
    widget = create_widget()
    subwindow = QtWidgets.QMdiSubWindow(self._mdiarea)
    subwindow.setWidget(widget)
    subwindow.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
    subwindow.show()
    subwindow.setProperty("center", True) # <----
    self._mdiarea.addSubWindow(subwindow)

def showEvent(self, event):
    if self.isVisible() and self._is_first_time:
        for subwindow in self._mdiarea.subWindowList():
            if subwindow.property("center"): # <---
                self.center_subwindow(subwindow)
        self._is_first_time = False