首页 文章

Pyqt5将小部件集中在一个小部件上

提问于
浏览
0

我正在尝试将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和父的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)

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

Edit:

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

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_())

2 回答

  • 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_()
    
  • 0

    我不太确定我的解决方案是否适合您,因为我使用它来将我的小部件集中在我的QApplication中 . 我发布了我的原始代码 . 希望能帮助到你

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

相关问题