Is there a way to center the screen created in python ( pyqt ) for any resolution?

Asked

Viewed 437 times

-1

I’m doing development of an application in python and Qt, ie pyqt5 and I wonder if there is a way whenever I run the display I created centralize on the screen, I saw that has to do this manually setting fixed values for top and left for example, but I would like the display to center on the screen in any resolution and I would like to know if this succeeds and if possible how I could do.

Note: I am beginner in both python and Qt.

2 answers

0

I use this small function in Mainwindow.

class MainWindow(QMainWindow):
      ...

      def center(self):
          qr = self.frameGeometry()
          cp = Lqt.QDesktopWidget().availableGeometry().center()
          qr.moveCenter(cp)
          self.move(qr.topLeft())

if __name__ == "__main__":
     app = QApplication([])
     dialog = MainWindow()
     dialog.center()
     dialog.show()
     sys.exit(app.exec_())

0

You can add this code to your Mainwindow:

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

You can also try the code below:

mainWindow.resize(mainWindow.sizeHint().width,
        mainWindow.size().height() + content.sizeHint().height());

Right after adding all the elements to the main screen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.