如何在屏幕上居中QMessageBox和QInputDialog?

2024-03-29 07:09:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个功能把一个物体放在屏幕中央。在

我想要一个QMainWindow,QInputDialog和QMessageBox居中。在

这是我的信箱:

def _Warning(self,_type):
        infoBox = QtWidgets.QMessageBox()
        infoBox.setIcon(QtWidgets.QMessageBox.Warning)
        infoBox.setWindowTitle("Warning")
        if (_type=="File"):
            infoBox.setText("The File Already exist in the current Directory")
        else:
            infoBox.setText("The Folder Already exist in the current Directory")

        self.center(infoBox)

        infoBox.exec_()

这是我的QInputDialog:

^{pr2}$

这是我的中心职能:

def center(self,object):
    qtRectangle = object.frameGeometry() 
    centerPoint = QtWidgets.QDesktopWidget().availableGeometry().center()
    qtRectangle.moveCenter(centerPoint)
    object.move(qtRectangle.topLeft())

我只能将Q主窗口居中。在

逻辑是将对象移到左上角(screenWidth/2-objectWidth/2,screenHeight/2-objectHeight/2),但我不知道我做错了什么。在


Tags: theselfobjectdeftypefilecenterwarning
1条回答
网友
1楼 · 发布于 2024-03-29 07:09:29

-Q消息框

QMessageBox的情况下,这是在exec_()方法中调整大小的,因此一个可能的解决方案是使用QTimer.singleShot()在显示后立即更改几何体。在

from functools import partial
from PyQt5 import QtCore, QtWidgets


def center(window):
    # https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen

    window.setGeometry(
        QtWidgets.QStyle.alignedRect(
            QtCore.Qt.LeftToRight,
            QtCore.Qt.AlignCenter,
            window.size(),
            QtWidgets.qApp.desktop().availableGeometry(),
        )
    )


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.btn_warning = QtWidgets.QPushButton(
            "Open QMessageBox", clicked=self.open_qmessagebox
        )

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.btn_warning)

        center(self)

    @QtCore.pyqtSlot()
    def open_qmessagebox(self):
        infoBox = QtWidgets.QMessageBox()
        infoBox.setIcon(QtWidgets.QMessageBox.Warning)
        infoBox.setWindowTitle("Warning")
        infoBox.setText("The XXX Already exist in the current Directory")
        wrapper = partial(center, infoBox)
        QtCore.QTimer.singleShot(0, wrapper)
        infoBox.exec_()


if __name__ == "__main__":
    import sys

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

-QInputDialog公司

对于QInputDialog,QInputDialog::getText()方法是静态的,因此“自我卡德罗对象不是窗口,因为窗口是在该方法中创建的。如果将父对象传递给getText(),则默认情况下,它将相对于该父对象居中。在

因此,如果QMainWindow是居中的,并且假设QMainWindow是自身,那么就没有必要修改任何东西。在

如果父对象不是在屏幕上居中,则有两种可能的解决方案:

  • 不要使用静态方法,而是通过QInputDialog实例实现逻辑:
^{pr2}$
  • 继续使用static方法并使用findChildren()获取窗口
from functools import partial
from PyQt5 import QtCore, QtWidgets


def center(window):
    # https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen

    window.setGeometry(
        QtWidgets.QStyle.alignedRect(
            QtCore.Qt.LeftToRight,
            QtCore.Qt.AlignCenter,
            window.size(),
            QtWidgets.qApp.desktop().availableGeometry(),
        )
    )


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.btn_inputdialog = QtWidgets.QPushButton(
            "Open QInputDialog", clicked=self.open_qinputdialog
        )

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.btn_inputdialog)

        center(self)

    @QtCore.pyqtSlot()
    def open_qinputdialog(self):
        parent = self
        dialogs = parent.findChildren(QtWidgets.QInputDialog)

        def onTimeout():
            dialog, *_ = set(parent.findChildren(QtWidgets.QInputDialog)) - set(dialogs)
            center(dialog)

        QtCore.QTimer.singleShot(0, onTimeout)
        text, okPressed = QtWidgets.QInputDialog.getText(
            parent, "New File", "File Name:", QtWidgets.QLineEdit.Normal, ""
        )
        if okPressed and text:
            print(text)


if __name__ == "__main__":
    import sys

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

相关问题 更多 >