Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Center the dialogue on the screen when main window is minimized
Qt 6.11 is out! See what's new in the release blog

Center the dialogue on the screen when main window is minimized

Scheduled Pinned Locked Moved Language Bindings
2 Posts 2 Posters 6.0k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    redstoneleo
    wrote on last edited by
    #1

    center the dialogue on the screen when main window is minimized
    When I run the following program ,I got a main window ,and after 2s a dialogue pops up centered over the main window ,next I select the yes button and then immediately minimized the main widow ,in this case after 2s the dialogue also pops up ,but this time it is in the taskbar .Can we center the dialogue on the screen in this case ?If possible ,how to write the code ?

    @
    import sys
    from PyQt4 import QtCore
    from PyQt4.QtGui import *
    class MainWindow(QMainWindow):
    def init(self):
    QMainWindow.init(self)
    self.resize(350, 250)
    self.setWindowTitle('mainwindow')

        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.showMessage)
        self.timer.start(2000)
        
    def showMessage(self):
        
        if True:
            
            msgBox = QMessageBox()
            msgBox.setText("The document has been modified.")
            msgBox.setInformativeText("Do you want to save your changes?")
            msgBox.setStandardButtons(QMessageBox.Yes|QMessageBox.No)
            msgBox.setDefaultButton(QMessageBox.Yes)
            reply = msgBox.exec_()
    
            if reply == QMessageBox.Yes:
                pass
            else:
                self.close()
    

    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

    @

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      I'm afraid I can't repeat your problem using your code on any of my systems (Mac, Windows7 or Linux), the dialog always comes up centered irrespective of the MainWindow state. What's your setup? You could try setting the dialog's parent to the MainWindow (i.e. QMessageBox(self)), its generally good practice and a good idea to give widgets a parent.

      As a more general answer to your question, the following example demonstrates centering a dialog on the screen:

      @
      from PyQt4.QtCore import *
      from PyQt4.QtGui import *

      class Dialog(QDialog):
      def init(self, parent=None):
      QDialog.init(self, parent)

          size=self.size()
          desktopSize=QDesktopWidget().screenGeometry()
          top=(desktopSize.height()/2)-(size.height()/2)
          left=(desktopSize.width()/2)-(size.width()/2)
          self.move(left, top)
      

      if name=="main":
      from sys import argv, exit
      a=QApplication(argv)
      d=Dialog()
      d.show()
      d.raise_()
      exit(a.exec_())
      @

      This will work for any QWidget derivative.

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      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