您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

QMessage@R_6_2419@方法中调整尺寸的情况下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 QMessage@R_6_2419@", clicked=self.open_qmessage@R_6_2419@
        )

        lay = QtWidgets.QV@R_6_2419@Layout(self)
        lay.addWidget(self.btn_warning)

        center(self)

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


if __name__ == "__main__":
    import sys

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

对于QInputDialog,QInputDialog :: getText()方法是静态的,因此“ self.cuadro”对象不是窗口,因为该窗口是在该方法中创建的。如果将父级传递给getText(),则认情况下它将相对于此居中。

因此,如果QMainWindow居中并假定QMainWindow是自身,则无需修改任何内容

相反,如果父母不在屏幕上居中,则有两种可能的解决方案:

不要使用静态方法并通过QInputDialog实例实现逻辑:

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 (self, parent=None): super().(parent)

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

    lay = QtWidgets.QV@R_6_2419@Layout(self)
    lay.addWidget(self.btn_inputdialog)

    center(self)

@QtCore.pyqtSlot()
def open_qinputdialog(self):
    dialog = QtWidgets.QInputDialog(self)
    dialog.setWindowTitle("New File")
    dialog.setLabelText("File Name:")
    dialog.setTextEchoMode(QtWidgets.QLineEdit.Normal)
    dialog.setTextValue("")
    wrapper = partial(center, dialog)
    QtCore.QTimer.singleShot(0, wrapper)
    text, okPressed = (
        dialog.textValue(),
        dialog.exec_() == QtWidgets.QDialog.Accepted,
    )
    if okPressed and text:
        print(text)

if == “”: import sys

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

继续使用静态方法,并使用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 (self, parent=None): super().(parent)

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

    lay = QtWidgets.QV@R_6_2419@Layout(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 == “”: import sys

app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
其他 2022/1/1 18:42:39 有741人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶