c++ - Qt: Overlapping semitransparent QgraphicsItem -


i've been working qgraphicsview while , facing requisite not sure if can fulfilled using framework.

putting simple possible, have 2 overlapping rectitem semitransparent qbrush (the same 1 both). possible prevent overlapping area becoming more opaque? want whole area have same color (this occur if both rects opaque, not case)

i know might seem weird requisite, old graphics engine colleagues used allowed it.

any ideas?

qt provides various blend (composition) modes qpainter. deriving rectitem class qgraphicsitem or qgraphicsobject, allows customise painting , using composition modes, create various effects, demonstrated in qt example.

if want 2 semi-transparent items overlapping without changing colour (assuming colour same), either qpainter::compositionmode_difference mode, or compositionmode_exclusion this. here's example code of such object: -

header

#ifndef rectitem_h #define rectitem_h  #include <qgraphicsitem> #include <qcolor>  class rectitem : public qgraphicsitem { public:     rectitem(int width, int height, qcolor colour);     ~rectitem();      qrectf boundingrect() const;  private:     qrectf m_boundingrect;     qcolor m_colour;      void paint(qpainter *painter, const qstyleoptiongraphicsitem *option, qwidget *widget = 0); };  #endif // rectitem_h 

implementation

#include "rectitem.h" #include <qpainter>  rectitem::rectitem(int width, int height, qcolor colour)     : qgraphicsitem(), m_boundingrect(-width/2, -height/2, width, height), m_colour(colour) {         setflag(qgraphicsitem::itemisselectable);     setflag(qgraphicsitem::itemismovable); }  rectitem::~rectitem() { }  qrectf rectitem::boundingrect() const {     return m_boundingrect; }  void rectitem::paint(qpainter *painter, const qstyleoptiongraphicsitem *, qwidget *) {     painter->setcompositionmode(qpainter::compositionmode_difference);     painter->setbrush(m_colour);     painter->drawrect(m_boundingrect); } 

you can create 2 rectitem objects of same semi-transparent colour , add them scene

// assuming scene , view setup , m_pscene pointer scene  rectitem* pitem = new rectitem(50, 50, qcolor(255, 0, 0, 128)); pitem->setpos(10, 10); m_pscene->additem(pitem);  pitem = new rectitem(50, 50, qcolor(255, 0, 0, 128)); pitem->setpos(80, 80); m_pscene->additem(pitem); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -