简体   繁体   English

Qt:重叠的半透明QgraphicsItem

[英]Qt: Overlapping semitransparent QgraphicsItem

I've been working with QGraphicsView for a while and I am facing a requisite that I am not sure if it can be fulfilled by using this framework. 我一直在使用QGraphicsView一段时间,我面临一个必要条件,我不确定它是否可以通过使用此框架来实现。

Putting it as simple as possible, I have 2 overlapping RectItem with a semitransparent QBrush (the same one for both). 尽量简单,我有2个重叠的RectItem和一个半透明的QBrush(两者都是相同的)。 Is it possible to prevent the overlapping area from becoming more opaque? 是否有可能防止重叠区域变得更不透明? I just want the whole area to have the same color (this will occur only if both rects are fully opaque, but sometimes that is not the case) 我只是希望整个区域具有相同的颜色(只有当两个区域完全不透明时才会出现,但有时情况并非如此)

I know it might seem a weird requisite, but the old graphics engine my colleagues used allowed it. 我知道这似乎是一个奇怪的必要条件,但我的同事使用的旧图形引擎允许它。

Any ideas? 有任何想法吗?

Qt provides various blend (composition) modes for the QPainter . Qt为QPainter提供各种混合(组合)模式。 Deriving your RectItem class from QGraphicsItem or QGraphicsObject, allows you to customise the painting and using the composition modes , create various effects, as demonstrated in the Qt Example . 从QGraphicsItem或QGraphicsObject派生RectItem类,允许您自定义绘画并使用合成模式 ,创建各种效果,如Qt示例中所示

If you want two semi-transparent items overlapping without changing the colour (assuming their colour is the same), either the QPainter::CompositionMode_Difference mode , or CompositionMode_Exclusion will do this. 如果您希望两个半透明项重叠而不更改颜色(假设它们的颜色相同),则QPainter :: CompositionMode_Difference模式或CompositionMode_Exclusion将执行此操作。 Here's example code of such an 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 now create two RectItem objects of the same semi-transparent colour and add them to the scene 您现在可以创建两个具有相同半透明颜色的RectItem对象,并将它们添加到场景中

// assuming the scene and view are setup and m_pScene is a pointer to the 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM