Centering a window on the screen with Qt
September 10th, 2007 by exhuma.twn
Yet another thing that’s not automagic in Qt.
Here’s a python solution:
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
dw = app.desktop().width()
dh = app.desktop().height()
myapp = MainWindow()
myapp.setGeometry(
int((dw - (dw - (dw / 2)) * 1.5) / 2),
int((dh - (dh - (dh / 2)) * 1.5) / 2),
int((dw - (dw / 2)) * 1.5),
int((dh - (dh / 2)) * 1.5))
myapp.show()
sys.exit(app.exec_())
app = QtGui.QApplication(sys.argv)
dw = app.desktop().width()
dh = app.desktop().height()
myapp = MainWindow()
myapp.setGeometry(
int((dw - (dw - (dw / 2)) * 1.5) / 2),
int((dh - (dh - (dh / 2)) * 1.5) / 2),
int((dw - (dw / 2)) * 1.5),
int((dh - (dh / 2)) * 1.5))
myapp.show()
sys.exit(app.exec_())
This will also resize the window, adapting to the desktop size.
Found and adapted from the Qt interest archive.
Posted in Python | No Comments »
