Ask Your Question
3

How can I center a window when using PySide6, QT, QML, and WSL2?

asked 2023-06-13 10:56:07 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-13 11:09:02 +0000

bukephalos gravatar image

You can center a window in PySide6 by using the QDesktopWidget class to get the size of the screen and then centering your window on it.

Here's an example:

from PySide6.QtGui import QDesktopWidget
from PySide6.QtWidgets import QApplication, QMainWindow

app = QApplication([])
window = QMainWindow()

# Set window dimensions
window.resize(800, 600)

# Center window on screen
desktop = QDesktopWidget()
rect = desktop.availableGeometry(desktop.primaryScreen())
x = int((rect.width() - window.width()) / 2)
y = int((rect.height() - window.height()) / 2)
window.move(x, y)

window.show()
app.exec_()

This should work regardless of whether you're using QT, QML, or WSL2.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-13 10:56:07 +0000

Seen: 11 times

Last updated: Jun 13 '23