Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pyqt5 QDialog not centered on mainWindow



  • I've created the main window ui and dialog ui in qt designer and I have also create the .py files.
    mainwindow:

    import sys
    from os.path import expanduser
    from PyQt5 import QtWidgets
    from PyQt5.QtCore import pyqtSlot
    from PyQt5.QtWidgets import QMainWindow, QFileDialog
    from mainwindow import Ui_mainWindow
    from msgDialog import MsgDialog
    
    class MainWindow(QMainWindow, Ui_mainWindow):
        def __init__(self, parent=None):
            """
            Constructor
            @param parent reference to the parent widget
            @type QWidget
            """
            super(MainWindow, self).__init__(parent)
            self.setupUi(self)
    
    def run():
        app = QtWidgets.QApplication(sys.argv)
        app.setQuitOnLastWindowClosed(True)
        ui = MainWindow()
        ui.show()
        return app.exec()
    

    dialog window:

    from PyQt5 import QtCore
    from PyQt5.QtCore import pyqtSlot
    from PyQt5.QtWidgets import QDialog
    from msgDialog_ui import Ui_MsgDialog
    
    class MsgDialog (QDialog, Ui_MsgDialog):
        def __init__(self, parent=None):
            super(MsgDialog, self).__init__(parent)
            self.setupUi(self)
            self.setWindowFlags(
                QtCore.Qt.CustomizeWindowHint |
                QtCore.Qt.FramelessWindowHint)
    

    This is the way these 2 widgets are constructed and connected with the ui/py files.
    This way works as I intended except that the dialog window changes its position when I move the main window on screen. It's not every time I called it to the main window's center. I searched about this and I found some relative topics, which suggest dialog to be child of main window, but I don't know how to make main window parent and dialog child.



  • @dancaer69
    Nothing in the code you show creates or shows the dialog.



  • Yes, I didn't put all code, just the constructors. The main window displayed from "run" method(I added it), and the dialog by pressing a button in main window with :

    msgdlg = MsgDialog(self)
    msgdlg.show()
    


  • @dancaer69 said in Pyqt5 QDialog not centered on mainWindow:

    msgdlg = MsgDialog(self)

    Because you passed self (the main window) as the parent to the constructor, you are doing the

    which suggest dialog to be child of main window, but I don't know how to make main window parent and dialog child.

    I notice you are using show(), not exec(), so this is a non-modal, modeless dialog. Meaning that you can interact with both the dialog & the main window. I don't know whether it is supposed to appear at the centre of its parent, but I think you can move them independently.



  • After a lot of searching and many tries I found this thread:
    https://stackoverflow.com/questions/12432740/pyqt4-what-is-the-best-way-to-center-dialog-windows
    which is the only that worked with some changes. It describes two methods. In both the dialog at least displayed in same position every time and doesn't move if I move the main window. The second one is the best because with this one the dialog is centered.
    So, this is the dialog' s class modification for future reference:

    from PyQt5.QtWidgets import QDialog
    import appMain
    
    from msgDialog_ui import Ui_MsgDialog
    
    class MsgDialog (QDialog, Ui_MsgDialog):
        def __init__(self, parent=None):
            super(MsgDialog, self).__init__(parent)
            self.setupUi(self)
            self.setWindowFlags(
                QtCore.Qt.CustomizeWindowHint |
                QtCore.Qt.FramelessWindowHint)
            self.centerPos()
    
        def centerPos(self):
            qr = appMain.MainWindow.window(self).frameGeometry()
            #cp = appMain.MainWindow.geometry(self).center()
            #qr.moveCenter(cp)
            #self.move(qr.center())
            x = (qr.width() - self.width()) / 2
            y = (qr.height() - self.height()) / 2
            self.move(x,y)
    

Log in to reply