Answer the question
In order to leave comments, you need to log in
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_())
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question