Jaitnium
Jaitnium

Reputation: 650

Pyqt5 Centering a widget on a widget

I'm trying to center a QMessageBox widget such that its center is aligned with the parent widget's center.

This just aligns the top left corners:

msgBox.move(parent.pos())

I did some math to try and align the centers based on the widgets' dimensions.

This aligns msgBox's top left corner to the parent's center:

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)

Continuing further, this should align the x axis of both msgBox and parent but msgBox is offset incorrectly

:

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)

Why is this not working properly and what is the correct solution? Thank you!

Edit:

Here is a simplified version of my program that gives the same results:

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

Upvotes: 3

Views: 10212

Answers (2)

eyllanesc
eyllanesc

Reputation: 243887

If you want to center a widget with respect to its parent, we assume that it places it in (0, 0) with respect to itself. And so you should use:

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

Instead if you want to center it with respect to another widget, you must use:

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

Upvotes: 2

Andrea
Andrea

Reputation: 603

I'm not pretty sure if my solution is good for you because I use it to center my widget inside my QApplication. I post my original code which works. Hope it helps

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

Upvotes: 0

Related Questions