What is the alternative? how to use QGuiApplication and QScreen to place my QMainWindow in the center of the screen.
What is the alternative? how to use QGuiApplication and QScreen to place my QMainWindow in the center of the screen.
You just have to use QGuiApplication.primaryScreen()(or QGuiApplication.screens()[i] if you want to center on the i-esima screen) as I show below:
from PyQt5 import QtCore, QtGui, QtWidgets
# or
# from PySide2 import QtCore, QtGui, QtWidgets
def center_window(widget):
window = widget.window()
window.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
window.size(),
QtGui.QGuiApplication.primaryScreen().availableGeometry(),
),
)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
w.resize(640, 480)
w.show()
QtCore.QTimer.singleShot(0, lambda: center_window(w))
sys.exit(app.exec_())