Results 1 to 2 of 2

Thread: How to center subwindows in qmdiarea?

  1. #1
    Join Date
    Apr 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to center subwindows in qmdiarea?

    Is there an attribute to position subwindows in qmdiarea? I’m trying to center subwindow in middle of mainwindow on startup (mdiarea)

    I’m working on a useful example but haven’t finished it, wanted to see if anyone has tried doing this, and how they did it

    Subwindows are randomly placed on startup when initialized


    Qt Code:
    1. class App(QMainWindow, Ui_MainWindow):
    2. def __init__(self, parent=None):
    3. QMainWindow.__init__(self, parent=parent)
    4. self.setupUi(self)
    5. self.screenShape = QDesktopWidget().screenGeometry()
    6. self.width = self.screenShape.width()
    7. self.height = self.screenShape.height()
    8. self.resize(self.width * .6, self.height * .6)
    9. self.new = []
    10. #calls GUI's in other modules
    11. self.lw = Login()
    12. self.vs = VS()
    13. self.ms = MS()
    14. self.hw = HomeWindow()
    15. self.mw = MainWindow()
    16. self.ga = GA()
    17. self.sGUI = Settings()
    18. # shows subwindow
    19. self.CreateLogin()
    20. self.CreateVS()
    21. self.CreateMS()
    22. self.CreateGA()
    23. self.CreateSettings()
    24.  
    25. def CreateLogin(self):
    26. self.subwindow = QMdiSubWindow()
    27. self.subwindow.setWidget(self.lw)
    28. self.subwindow.setAttribute(Qt.WA_DeleteOnClose, True)
    29. self.mdiArea.addSubWindow(self.subwindow)
    30. self.subwindow.setMaximumSize(520, 300)
    31. self.subwindow.setMinimumSize(520, 300)
    32. self.lw.showNormal()
    33.  
    34. def CreateVS(self):
    35. self.subwindow = QMdiSubWindow()
    36. self.subwindow.setWidget(self.vs)
    37. self.mdiArea.addSubWindow(self.subwindow)
    38. self.vs.showMinimized()
    39.  
    40. def CreateMS(self):
    41. self.subwindow = QMdiSubWindow()
    42. self.subwindow.setWidget(self.ms)
    43. self.mdiArea.addSubWindow(self.subwindow)
    44. self.ms.showMinimized()
    45. self.ms.tabWidget.setCurrentIndex(0)
    46.  
    47. def CreateGA(self):
    48. self.subwindow = QMdiSubWindow()
    49. self.subwindow.setWidget(self.ga)
    50. self.mdiArea.addSubWindow(self.subwindow)
    51. self.ga.showMinimized()
    52. self.subwindow.setMaximumSize(820, 650)
    53.  
    54. def CreateSettings(self):
    55. self.subwindow = QMdiSubWindow()
    56. self.subwindow.setWidget(self.sGUI)
    57. self.mdiArea.addSubWindow(self.subwindow)
    58. self.sGUI.showMinimized()
    59.  
    60. def CreateWindow(self):
    61. self.hw.pushButton.clicked.connect(self.vs.showNormal)
    62. self.hw.pushButton_2.clicked.connect(self.Moduleprogram)
    63. self.hw.pushButton_3.clicked.connect(self.ms.showNormal)
    64. self.hw.pushButton_4.clicked.connect(self.ga.showNormal)
    65. self.subwindow = QMdiSubWindow()
    66. self.subwindow.setWindowFlags(Qt.CustomizeWindowHint | Qt.Tool)
    67. self.subwindow.setWidget(self.hw)
    68. self.subwindow.setMaximumSize(258, 264)
    69. self.subwindow.move(self.newwidth*.35, self.newheight*.25)
    70. self.mdiArea.addSubWindow(self.subwindow)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to center subwindows in qmdiarea?

    No widget geometry will be correct until after the widget's showEvent(), so you can't rely on any geometry calculations made in the constructor (__init__). You also cannot rely on any calculations in the resizeEvent() prior to the window's showEvent().

    So after the main window has been shown (isVisible() returns true), you can then get the geometry. The geometry you want is that of the QMdiArea. Find its center by retrieving its rect() and then the center() of that. To center your QMdiSubWindow, get its frameGeometry() (or use whatever size you intend to create it with). Divide the height() and width() of that in half, subtract the half-width from the x coordinate of the MDI area's center and the half-height from the y coordinate of the MDI center, and use the result as the argument to setPos() on the MDI subwindow. This will give you an MDI area and an MDI subwindow that have the same center coordinate, that is, the subwindow will be centered in the MDI area. Once that's done, you can show() the subwindow.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QMdiArea Problems with Menu Bar when subWindows are maximized
    By BettaUseYoNikes in forum Qt Programming
    Replies: 1
    Last Post: 6th July 2021, 06:17
  2. Replies: 0
    Last Post: 31st January 2016, 13:40
  3. Resize MDI subwindows
    By 8Observer8 in forum Newbie
    Replies: 7
    Last Post: 21st August 2014, 10:24
  4. Center image on QMdiArea background
    By cia.michele in forum Qt Programming
    Replies: 0
    Last Post: 5th October 2010, 14:02
  5. Replies: 5
    Last Post: 15th June 2010, 22:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.