PyQt5 windows overlapping on startup and button press

43 Views Asked by At

Edit: ended up shifting to QDockWidgets and using splitDockWidget for organization after reading -musicamante's link, which tldr:x11 doesn't play nice with setting size or positioning windows. Similar functionality and close enough for what I want to do. Thanks!

Using PyQt5, I'm attempting to resize and move 5 individual windows to default locations and sizes at app startup and upon a button press. The 5 windows should be arranged in 3 columns, with 2 of the columns being split unevenly top and bottom. The two problems are, there is some minor overlap among the windows horizontally, which I feel I can safely assume is due to the taskbar being placed at the left edge of my screen (Ubuntu lts 22.04, default) and in the columns with multiple windows, the windows are both stacked atop each other at the top of the screen. This second error is the primary irritation. What I believe to be the relevant code follows:

#image.py

class ImageWindow(QMainWindow):

    def __init__(self):

        super().__init__()

        self.image_widget = ImageWidget()

        self.setCentralWidget(self.image_widget)

        # Fetch screen dimensions

        screen = QGuiApplication.primaryScreen()

        screen_size = screen.availableGeometry()

        # Calculate default size and position

        self.default_width = int(screen_size.width() * 0.4)

        self.default_height = int(screen_size.height() * 0.5)

        self.default_pos_x = int(screen_size.width() * 0.2)

        self.default_pos_y = int(screen_size.height() * 0.5)

        print(f"Default Width: {self.default_width}")

        print(f"Default Height: {self.default_height}")

        print(f"Default X Position: {self.default_pos_x}")

        print(f"Default Y Position: {self.default_pos_y}")

        self.setWindowTitle("Image Viewer")

        self.set_to_default()

    def set_to_default(self):

        # Reset size and position to defaults

        self.setGeometry(self.default_pos_x, self.default_pos_y, self.default_width, self.default_height)


Other windows all approximate this one. Print statements all appear to be displaying appropriate information.

Tried resetting default immediately after show() in my main.py:



imageWindow.show()

imageWindow.set_to_default()


Did nothing.

Tried injecting an artificial delay into each window:



    def set_geometry():

        self.resize(self.default_width, self.default_height)

        self.move(self.default_pos_x, self.default_pos_y)

    QTimer.singleShot(100, set_geometry)


Did nothing, but 100ms slower.

Tried breaking resize and move into 2 parts:

self.resize(self.default_width, self.default_height)
self.move(self.default_pos_x, self.default_pos_y)

Did what it always does.

I'm out of ideas.

0

There are 0 best solutions below