Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Pyqt5 QDialog not centered on mainWindow

Pyqt5 QDialog not centered on mainWindow

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dancaer69
    wrote on last edited by dancaer69
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • D dancaer69

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dancaer69
        wrote on last edited by
        #3

        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()
        
        JonBJ 1 Reply Last reply
        0
        • D dancaer69

          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()
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          2
          • D Offline
            D Offline
            dancaer69
            wrote on last edited by
            #5

            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)
            
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved