2017-06-23 7 views
0

私は、その中心が親ウィジェットの中心に揃うようにQMessageBoxウィジェットを中心に配置しようとしています。Pyqt5ウィジェットでウィジェットを中心にする

これはちょうど左上の角を揃え:

msgBox.move(parent.pos()) 

私がしようとすると、ウィジェット寸法に基づいてセンターを揃えるためにいくつかの数学をしました。

これは親の中央にメッセージボックスの左上隅揃え:さらに継続

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軸を揃える必要がありますが、メッセージボックスが間違って

オフセットされます

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

は、あなたがそれを投げたときにのみ集中したいかまたはあなたがしたいですかそれは常に中央にあるのですか? – eyllanesc

+0

私はそれを投げるときだけ。 – Jaitnium

+0

私の答えを参照してください、 – eyllanesc

答えて

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

これは、親がどこにあるかにかかわらずmsgBoxを画面の左上隅の近くに移動します。私はrect()。center()は四角形の中心を示しますが、スクリーン座標とは異なる座標系を与えていると思います。 – Jaitnium

+1

QMessageboxオブジェクトの作成方法や、テスト可能なコードを表示することができます。 – eyllanesc

+1

私の親と呼ぶものは別のウィジェットだと思います。 – eyllanesc

0

私はそれを使用するので、私の解決策は、あなたのために良好であればかなりわからないんだけど私のウィジェットを私のQApplicationの中に入れる。私は元のコードを掲載しています。あなたがその親に対するウィジェットを中心にしたい場合は、

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

私は応答を感謝します! 2つのウィジェットで作業するようにコードを修正しましたが、それは正しくありません。msgBoxはデスクトップの左上隅に中央揃えになります。私はまだそれを見ている。 – Jaitnium

関連する問題