0

I am using PySide2 to paint my widget. Everything works, but at one point inside my paintEvent, I want to take the result of my painting, apply a blur to it and then comp it with the original. Is this possible?

I read that QGraphicsEffect is only applicable to widgets, so I found something like this:

def applyEffectToImage(src, effect):
  scene = QtWidgets.QGraphicsScene()
  item = QtWidgets.QGraphicsPixmapItem()
  item.setPixmap(QPixmap.fromImage(src))
  item.setGraphicsEffect(effect)
  scene.addItem(item)
  res = QImage(src.size(), QImage.Format_ARGB32)
  res.fill(Qt.transparent)
  ptr = QPainter(res)
  scene.render(ptr, QRectF(), QRectF(0,0, src.width(), src.height()) )
  return res

but even with this, which allows me to call a QGraphicsEffect inside my paintEvent, because my initial source is QPainter(self), how can I convert this into a QImage?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • The idea of QGraphicsEffect is precisely not to have the need to override paintEvent to apply the effect. From your question I understand that your goal is to apply the effect on some QWidget (or derivatives) – eyllanesc Jan 29 '20 at 16:05
  • No actually my main goal is to apply an effect to an image or QImage so I can do some basic comp while I am drawing my widget, that's why I am asking this question. Because if I can't do it inside my paintEvent, that means I will have to create multiple widgets and stack them on top of each other, because I have multiple layers where the other layers are the result of an effect. – Joan Venge Jan 29 '20 at 16:07
  • According to what you say you have an XY problem, why do you assume that if you can't paintEvent then you can't achieve your main goal? It seems to me that you do not know the objective of QGraphicsEffect a bit, the objective of QGraphicsEffect is to modify the painting of QGraphicsItems or QWidget without having to create new classes or override the paint or paintEvent method, respectively, since the traditional method implies having to inherit of the widget and modify paintEvent, and that is not scalable. – eyllanesc Jan 29 '20 at 16:13
  • Ok then please tell me how I can draw a shape (A), then draw another shape (B), blur B (C), then comp B over C, over A, where A and B is not affected by the graphic effect, inside a single widget? – Joan Venge Jan 29 '20 at 16:23
  • @eyllanesc: Also I applied the highlight effect you posted, but it applies the effect over the whole widget. The graphics I paint is a small section of the entire widget, so I want to apply the effect only there, not around the whole widget. Is there a way to do that? – Joan Venge Jan 29 '20 at 16:37
  • Could you please explain to me exactly what you want, your last 2 comments in this post are often confusing (they are far from the question of your publication). In addition, your publication is confusing because you talk about widgets (QWidgets and derivatives) but in your code you use item (QGraphicsItems or derivatives). – eyllanesc Jan 29 '20 at 19:26
  • If you rewrite your question by pointing out a clear and concise question then you could give yourself a concrete answer. For example, in one of the comments you seem to ask how to apply several QGraphicsEffect in cascade, and another very different question is how to apply a QGraphicsEffect in an internal and small section of the QWidget. Which of those 2 questions is that of this post? It is understood that if you have 2 questions then you must create 2 post. – eyllanesc Jan 29 '20 at 19:26
  • @eyllanesc: it's something like this: https://imgur.com/a/b8fvaW1. As you can see this is the whole widget, so if I apply the effect to the widget, the whole widget will get the effect i.e. glow/blur. I want to only apply it to a certain element that I painted which is the rectangle in the center. That's why I wanted to paint the rectangle in paintEvent, apply glow/blur to it, put the original rectangle on top, then continue to paint the other elements (line on the left and the circles) which also need to be unaffected. So how can I do this without doing it in paintevent or multiple widgets? – Joan Venge Jan 30 '20 at 04:50
  • Your current question (post) is misspelled, however your last comment is clearer, so I recommend you rewrite your post with the new information. In addition, your comment and your post contradict at least the following point: *... inside paintEvent ...* and *... without doing it in paintevent ...* With or without paintEvent ?, to use QPainter no It is necessary to implement a QPaintEvent, it is only necessary to use QPaintEvent when you paint a widget but if you paint a QPixmap, QImage, etc. it is not necessary. – eyllanesc Jan 30 '20 at 06:07
  • Your question should be clearer: How to apply a QGraphicsEffect in a QPainter instruction (instructions: fillRect, drawEllipse, etc) ?, but also analyzing your question you also keep confusing since in your example you use QGraphicsItems. I think you don't know much about Qt, so I'm going to illustrate a bit: QPainter is a painter who paints on various devices (QPixmap, QImage, QWidget, QGraphicsItem). QWidget and QGraphicsItem are 2 very different things: they are 2 different technologies. – eyllanesc Jan 30 '20 at 06:12
  • @eyllanesc: I said without doing it in paintevent because you said before I don't need to use a paintevent, that's why I am asking if you can show it. "How to apply a QGraphicsEffect in a QPainter instruction (instructions: fillRect, drawEllipse, etc)" That's not applying a QGraphicsEffect. Read https://doc.qt.io/archives/qt-4.8/qgraphicseffect.html. I want to apply an effect like QGraphicsBlurEffect. You are showing me how to paint/draw using QPainter. I am asking how to apply effect inside a paint event. Sorry but I don't think you understand what I am asking here. – Joan Venge Jan 30 '20 at 07:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206901/discussion-between-joan-venge-and-eyllanesc). – Joan Venge Jan 30 '20 at 07:16

0 Answers0