【问题标题】:Pyqt5 Centering a widget on a widgetPyqt5在一个小部件上居中一个小部件
【发布时间】:2019-03-11 12:04:00
【问题描述】:

我正在尝试将 QMessageBox 小部件居中,使其中心与父小部件的中心对齐。

这只是对齐左上角:

msgBox.move(parent.pos())

我做了一些数学运算,尝试根据小部件的尺寸对齐中心。

这会将 msgBox 的左上角与父级的中心对齐:

x1 = parent.frameGeometry().width()
y1 = parent.frameGeometry().height()

#Offset msgBox by half of parent's width and height
msgBox.move(parent.pos().x() + x1/2, parent.pos().y() + y1/2)

继续,这应该对齐 msgBox 和 parent 的 x 轴,但 msgBox 偏移不正确

:

x1 = parent.frameGeometry().width()
y1 = parent.frameGeometry().height()

x2 = msgBox.frameGeometry().width()
y2 = msgBox.frameGeometry().height() 

#Offset msgBox by half of parent's width and height
#Then offset msgBox back by half of its width
msgBox.move(parent.pos().x() + x1/2 - x2/2, parent.pos().y() + y1/2)

为什么这不能正常工作,正确的解决方案是什么?谢谢!

编辑:

这是我的程序的简化版本,它给出了相同的结果:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_Form(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)

    def infoBox(self):
        #Create Info box
        infoBox = QtWidgets.QMessageBox()
        infoBox.setIcon(QtWidgets.QMessageBox.Warning)
        infoBox.setText("Warning:")
        message = "Blank entries indicate no corresponding Misc word and will be omitted from the output file."
        message += "\n\nConvert anyway?"
        infoBox.setInformativeText(message)
        infoBox.setWindowTitle("Warning")
        infoBox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)

        #Position infoBox
        infoBox.move(self.rect().center())

        #Execute infoBox
        reply = infoBox.exec_()

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 400)

        self.main_Layout = QtWidgets.QVBoxLayout(Form)
        self.main_Layout.setObjectName("main_Layout")

        #Add b utton
        self.button = QtWidgets.QPushButton(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.button.setFont(font)
        self.button.setObjectName("saveDefault")
        self.main_Layout.addWidget(self.button)


        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Test"))

        self.button.setText(_translate("Form", "Warning"))
        self.button.clicked.connect(self.infoBox)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

【问题讨论】:

  • 只有在我扔的时候。

标签: python pyqt pyqt5


【解决方案1】:

如果您想将一个小部件相对于其父级居中,我们假设它相对于自身将其放置在 (0, 0) 中。所以你应该使用:

infoBox = QtWidgets.QMessageBox(self)
# ...
#Position infoBox
msgBox.move(parent.rect().center())
#Execute infoBox
reply = infoBox.exec_()

如果您想将其相对于另一个小部件居中,则必须使用:

infoBox = QtWidgets.QMessageBox() 
# ...
#Position infoBox
p = another_widget.frameGeometry().center() - QtCore.QRect(QtCore.QPoint(), infoBox.sizeHint()).center() 
infoBox.move(p)
#Execute infoBox
reply = infoBox.exec_()

【讨论】:

  • 这会将 msgBox 移动到屏幕左上角附近,而不管父级在哪里。我认为 rect().center() 给出了矩形的中心,但在与屏幕坐标不同的坐标系中。
  • 您可以展示如何创建 QMessagebox 对象,或者可以对其进行测试的代码。
  • 我认为你对我来说所谓的父母是另一个小部件。
  • 检查我的编辑。它只是一个带有打开 QMessagebox 按钮的窗口。
【解决方案2】:

我不太确定我的解决方案是否适合您,因为我使用它来将我的小部件置于 QApplication 中的中心。我发布了有效的原始代码。希望对你有帮助

msgBox.move(self.app.desktop().screen().rect().center() - msgBox.rect().center())

【讨论】:

  • 感谢您的回复!我修改了您的代码以使用两个小部件,但它并不完全正确 - msgBox 与桌面的左上角居中对齐。不过我还在看。
猜你喜欢
  • 1970-01-01
  • 2021-05-28
  • 2021-03-17
  • 2012-08-05
  • 2011-01-18
  • 2022-10-24
  • 1970-01-01
  • 2019-01-30
  • 2014-09-01
相关资源
最近更新 更多