Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. CompositionMode in between QGraphicsItem

CompositionMode in between QGraphicsItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicsviewqgraphicsitemqpainterrendering
5 Posts 2 Posters 889 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sanjuchopracool
    wrote on 20 May 2023, 09:38 last edited by sanjuchopracool
    #1

    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?

    J 1 Reply Last reply 20 May 2023, 09:45
    0
    • S sanjuchopracool
      20 May 2023, 09:38

      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?

      J Offline
      J Offline
      JonB
      wrote on 20 May 2023, 09:45 last edited by JonB
      #2

      @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 for qgraphicsview composition.

      S 1 Reply Last reply 20 May 2023, 10:43
      0
      • J JonB
        20 May 2023, 09:45

        @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 for qgraphicsview composition.

        S Offline
        S Offline
        sanjuchopracool
        wrote on 20 May 2023, 10:43 last edited by sanjuchopracool
        #3

        @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
        QT_BLEND.png

        inkscape does not consider canvas background for blending
        Inkscape.png

        #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()
        {
        }
        
        
        
        J 1 Reply Last reply 20 May 2023, 10:53
        0
        • S sanjuchopracool
          20 May 2023, 10:43

          @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
          QT_BLEND.png

          inkscape does not consider canvas background for blending
          Inkscape.png

          #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()
          {
          }
          
          
          
          J Offline
          J Offline
          JonB
          wrote on 20 May 2023, 10:53 last edited by
          #4

          @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:

          ?

          S 1 Reply Last reply 20 May 2023, 11:08
          0
          • J JonB
            20 May 2023, 10:53

            @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:

            ?

            S Offline
            S Offline
            sanjuchopracool
            wrote on 20 May 2023, 11:08 last edited by
            #5

            @JonB thanks for googling :) . I have seen some more example. In case of GraphicsView fill transparent on scene did not help. I am pretty sure there is some way to do it, need to google more

            1 Reply Last reply
            1

            1/5

            20 May 2023, 09:38

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved