3

I have inherited from QGraphicsPolygonItem and would like to draw a QPixmap on the top of the item. The item itself and the pixmap should be drawn with rounded corners.

How would you this?

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
  • Having consulted Meta Stackoverflow, I have reconsidered closing this question, based upon this discussion: http://meta.stackoverflow.com/questions/250204/can-you-answer-your-own-questions-on-stack-overflow , though I agree with @DominikSelzer that this was something that should have been a simple google search. – TheDarkKnight Jun 13 '14 at 14:15
  • Mentioned answer is from 2011 and this one is proved in the last half and hour. – Alexander Tyapkov Jun 13 '14 at 14:40
  • Dominic, I don't argue that my answer is different. What I wanted is just to share short, explicit and actual answer to the question which cannot be found on this resource. Also if you found the answer on the first hit, then probably because you know the question already? I was working in different field and the question was not obvious by the moment. Anyway, if moderators consider this question to be bad, then there is always possibility to remove it. – Alexander Tyapkov Jun 13 '14 at 15:03
  • 2
    I am glad you found the answer to your problem. We are all here to learn and help. But i have to gentle disagree when posting questions that are extremly related to questions that are already on SO or duplicates from other well known resources. Another point is, that at least i think (and i might be biased) that this question can even be answered when reading the API (how to use Brushes). – OnWhenReady Jun 13 '14 at 15:14
  • @DominikSelzer IMO I don't think "It's in the docs" is a good reason by itself to close a question. We have a [2.7k point question on JavaScript's "use strict"](http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it) which is also found in the JS docs. If it's "extremely related to questions that are already on SO", then flag it as a duplicate. – Colonel Thirty Two Jun 13 '14 at 16:50

1 Answers1

5

If you want to draw QPixmap with rounded corners then inside of paint event create QBrush from QPixmap and apply it to the painter like that:

painter->setRenderHint(QPainter::Antialiasing, true);
QBrush brush = QBrush(pixmap);
painter->setBrush(brush);
painter->drawRoundedRect(polygon().boundingRect(), 5, 5);
Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
  • 3
    Why are you asking a question to which you answer immediately and obviously know the answer? – TheDarkKnight Jun 13 '14 at 13:57
  • Mark your answer as appropriate answer. And you'll get points. But that is unfair. – NG_ Jun 13 '14 at 13:58
  • 4
    @DominikSelzer actually [it is allowed and encouraged](http://stackoverflow.com/help/self-answer) – ratchet freak Jun 13 '14 at 14:31
  • I don't think it is encouraged to post an question/answer combination when using google gives the answer as a first hit. I think it is encouraged to first search for an answer. qtcentral has already documented the answer (see my comment on the question). Furthermore, even on SO there is already an extremely related answer to this question (http://stackoverflow.com/questions/6507511/qt-round-rectangle-why-corners-are-different). – OnWhenReady Jun 13 '14 at 14:43
  • @Merlin069: I think the general trend seems that on Stack Overflow if the information is useful, it is OK because the primary person of the site is about building knowledge and the corresponding people are just secondary. – László Papp Jun 13 '14 at 17:12
  • I have this issue with PyQt, can anyone help me with that? – Amin Khormaei Apr 07 '23 at 20:53