M
M
milssky2019-07-17 20:25:46
Python
milssky, 2019-07-17 20:25:46

How to position fullscreen widget in the center of the screen?

The question in itself sounds silly, but nevertheless. We have this code:

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.screenshot = QApplication.primaryScreen().grabWindow(0)
        self.settings()
        self.create_widgets()
        self.set_layout()

    def settings(self):
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setWindowState(QtCore.Qt.WindowFullScreen)

    def create_widgets(self):
        self.image_preview = QLabel()
        self.image_preview.setPixmap(self.screenshot)

    def set_layout(self):
        self.layout = QGridLayout(self)
        self.layout.addWidget(self.image_preview, 0, 0, alignment=QtCore.Qt.AlignCenter)
        self.setLayout(self.layout)

We take a screenshot of the screen, and in QLabel we display it on the screen without frames and buttons. And we get a slightly moved window to the right down, as in the screenshot (white frame on the left and top).
  1. Is it about QGridLayout?
  2. How to fix?

5d2f589558071577721096.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
milssky, 2019-07-18
@milssky

The answer is simple. It was necessary to adjust the padding of the content field.
self.layout.setContentsMargins(0, 0, 0, 0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question