Center the dialogue on the screen when main window is minimized
-
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_())@
-
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.