CompositionMode in between QGraphicsItem
-
Programs like inkscape have layers and objects. We can specify composition mode (blending mode) between layers and objects. I was thinking to implement a small prototype for vector drawing using QGraphicsView framework for canvas, but I could not find any way to specify composition for graphics item.
Is there any way to specify composition mode between overlapping graphics items?
-
@sanjuchopracool
I know little about "composition mode (blending mode)". But does https://stackoverflow.com/a/31406755/489865 in question Qt: Overlapping semitransparent QgraphicsItem do what you are looking for? There are a few other Google hits forqgraphicsview composition
. -
@JonB Thanks for the example. But there is one problem. Let say one rectangle is partially overlapping with other. I want only the overlapping region to blend. Non overlapping region should be drawn normally. With QGraphicsItem non overalpping item is blending with Scene background. I don't want that. (it works if I set scene color to white, But I don't want to set white color).
Qt
inkscape does not consider canvas background for blending
#include "widget.h" #include <QGraphicsItem> #include <QGraphicsScene> #include <QColor> #include <QGraphicsView> #include <QVBoxLayout> class RectItem : public QGraphicsItem { public: RectItem(int width, int height, QColor colour, QPainter::CompositionMode mode = QPainter::CompositionMode_SourceOver); ~RectItem(); QRectF boundingRect() const; private: QRectF m_boundingRect; QColor m_colour; QPainter::CompositionMode m_mode; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); }; RectItem::RectItem(int width, int height, QColor colour, QPainter::CompositionMode mode) : QGraphicsItem(), m_boundingRect(-width/2, -height/2, width, height), m_colour(colour), m_mode(mode) { setFlag(QGraphicsItem::ItemIsSelectable); setFlag(QGraphicsItem::ItemIsMovable); } RectItem::~RectItem() { } QRectF RectItem::boundingRect() const { return m_boundingRect; } void RectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { painter->save(); painter->setCompositionMode(m_mode); painter->setPen((Qt::NoPen)); painter->setBrush(m_colour); painter->drawRect(m_boundingRect); painter->restore(); } Widget::Widget(QWidget *parent) : QWidget(parent) { auto pView = new QGraphicsView(); QVBoxLayout * layout = new QVBoxLayout(); layout->addWidget(pView); setLayout(layout); QGraphicsScene* pScene = new QGraphicsScene(); RectItem* pItem = new RectItem(50, 50, QColor(255, 0, 0)); pItem->setPos(10, 10); pScene->addItem(pItem); pItem = new RectItem(50, 50, QColor( 0, 0, 255), QPainter::CompositionMode_Multiply); pItem->setPos(80, 80); pScene->addItem(pItem); pView->setScene(pScene); } Widget::~Widget() { }
-
@sanjuchopracool
Then perhaps you need to do some more Goggling than I did!Bear in mind I have said I know little about this, you know more than I! For your situation does https://stackoverflow.com/a/57215900/489865 give you a clue for something you can do
The composition mode works on transparent backgrounds, in your case it is not, so you must set it before painting, for this you could use the fill() method:
?