A
A
Andrey Myvrenik2015-12-14 23:14:41
Python
Andrey Myvrenik, 2015-12-14 23:14:41

What could cause a segmentation fault when trying to fill a widget with a gradient in PyQt5?

I needed to fill a widget with a gradient in PyQt5, but every time I try to do this, the program ends with a segmentation fault. With that, it is not necessary to fill it, it is enough even to pass the QGradient object to the QBrush constructor . A simple program code for an example:

from sys import exit, argv
from PyQt5 import QtGui, QtWidgets

class MyWidget(QtWidgets.QWidget):
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        grad = QtGui.QGradient()
        grad.setColorAt(0.0, QtGui.QColor(0, 0, 0))
        grad.setColorAt(1.0, QtGui.QColor(0, 0, 100))

        painter.fillRect(event.rect(), grad) # segfault
        #brush = QtGui.QBrush(grad) # segfault
        #painter.fillRect(event.rect(), QtGui.QColor(100, 0, 0)) # ok

if __name__ == '__main__':
    app = QtWidgets.QApplication(argv)
    window = MyWidget()
    window.show()
    exit(app.exec_())

What have I done wrong? Has anyone come across this? I would also be overly grateful if you can test this code on your own machine and leave a comment if your program crashes with a segmentation fault or not.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Myvrenik, 2015-12-15
@gim0

The problem, as it turned out, was this line:
I apparently used the wrong class. Replacing with QLinearGradient helped.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question