Qt:重叠的半透明的QgraphicsItem [英] Qt: Overlapping semitransparent QgraphicsItem

查看:3692
本文介绍了Qt:重叠的半透明的QgraphicsItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用QGraphicsView一段时间,我面对一个必要条件,我不知道是否可以通过使用这个框架来实现。

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.

它尽可能简单,我有2重叠的RectItem与半透明的QBrush(两者相同)。是否可以防止重叠区域变得更加不透明?我只想让整个区域有相同的颜色(这将发生只有当两个矩形是完全不透明,但有时不是这种情况)

Putting it as simple as possible, I have 2 overlapping RectItem with a semitransparent QBrush (the same one for both). 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)

我知道它可能

任何想法?

推荐答案

Qt提供了各种混合(组合)模式,用于 QPainter 。从QGraphicsItem或QGraphicsObject派生RectItem类,允许您自定义绘画并使用组合模式,创建各种效果,如 Qt示例

Qt provides various blend (composition) modes for the 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.

如果您希望两个半透明项目在不更改颜色的情况下重叠(假设它们的颜色相同),则 QPainter :: CompositionMode_Difference mode ,或CompositionMode_Exclusion将执行此操作。以下是此类对象的示例代码: -

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. 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);
}

现在,您可以创建两个相同半透明颜色的RectItem对象,并添加他们到现场

You can now create two RectItem objects of the same semi-transparent colour and add them to the scene

// 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);

这篇关于Qt:重叠的半透明的QgraphicsItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆