QT QOpenGLWidget size is wrong

57 views Asked by At

I'm at my wits end. I've been trying for a couple of days and can't seem to figure out what I'm doing wrong. I assume I am doing something wrong since I can't find any other references to my problem. When I override the paintEvent in QOpenGLWidget the size of the widget is always (100, 30) even though I set the min and max height to 48 in QT Designer. and put no limit on the width. Here's the init code:

from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
from components.PaletteBar import PaletteBar


Form, Window = uic.loadUiType("qt/main.ui")

app = QApplication([])
window = Window()
form = Form()
form.setupUi(window)
palette = PaletteBar(form.centralwidget) # A python class that subclasses QOpenGLWidget
window.show()
app.exec_()

Here's some of the widget code:

import math
from PyQt5.QtWidgets import QOpenGLWidget
from PyQt5.QtGui import QPainter, QColor, QPaintEvent, QResizeEvent, QOpenGLContext, QSurfaceFormat
from PyQt5.QtCore import QRect
from fractal.Palette import Palette


class PaletteBar(QOpenGLWidget):
    _my_palette: Palette = None

    def __init__(self, parent):
        super(PaletteBar, self).__init__(parent)

        self._my_palette = Palette({})

    def initializeGL(self) -> None:
        pass

    def paintEvent(self, event:QPaintEvent):
        bounds: QRect = event.rect()
        self.update_view(bounds)

    def paintGL(self):
        pass

    def resizeGL(self, width, height):
        bounds: QRect = QRect(0, 0, width, height)
        self.update_view(bounds)

    def resizeEvent(self, event:QResizeEvent):
        size = event.size()
        bounds: QRect = QRect(0, 0, size.width(), size.height())
        self.update_view(bounds)

    def update_view(self, bounds: QRect):
        painter = QPainter()
        painter.begin(self)

        num_colors = math.floor(len(self.my_palette.colors) / self.my_palette.color_range)
        if num_colors == 0:
            return

        width = math.ceil(bounds.width() / num_colors)
        height = bounds.height()

        for idx in range(num_colors):
            color = self.my_palette.colors[idx]
            x = idx * width
            rect = QRect(x, 0, x + width, height)
            painter.fillRect(rect, color)

        painter.end()

resizeEvent gets called at start-up and the event.size value is (100, 30), it never gets called again even when resizing the whole app.

paintEvent gets called at start-up and the event.rect is (0, 0, 100, 30). This event seems to get called frequently but the size is always the same.

paintGL and resizeGL are never called.

I feel like I must be doing something obviously wrong, I hope one of you gurus spots it.

0

There are 0 answers