Pergunta

This code works, but I wonder if there is any simpler way:

def center(self):
    qr = self.frameGeometry()
    cp = gui.QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())
Foi útil?

Solução

No, it's the simplest way. Here is a snippet I've used in C++:

  QRect desktopRect = QApplication::desktop()->availableGeometry(this);
  QPoint center = desktopRect.center();

  move(center.x() - width() * 0.5, center.y() - height());

Outras dicas

just add this line to your main windows :

self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())
self.move(QDesktopWidget().availableGeometry().center() - self.frameGeometry().center())

Just another "func-style" example. If you use it several times.

screen_center = lambda widget: QApplication.desktop().screen().rect().center()- widget.rect().center()

And every time in code:

widget.move(screen_center(widget))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top