我有一个QGraphicsSvgItem
子类,我想修改svg颜色。我想用QGraphicsColorizeEffect
,它很有用。
我的项目还有一个自定义选择矩形,突出显示 - 与其他项目类型类似。
当我应用着色效果时,高光也会变成相同的颜色......
我在绘画中试过setEnabled(false);
,但似乎没有效果。
示例代码:
文件mysvg.h
#ifndef MYSVG_H
#define MYSVG_H
#include <QGraphicsSvgItem>
#include <QGraphicsColorizeEffect>
class MySvg : public QGraphicsSvgItem
{
public:
MySvg();
~MySvg();
virtual void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget = NULL);
private:
QGraphicsColorizeEffect* m_effect;
void drawSelectionRectangle(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRectF& rectangle);
};
#endif // MYSVG_H
文件mysvg.cpp
#include <QStyleOptionGraphicsItem>
#include <QStyle>
#include <QPainterPath>
#include <QPainter>
#include <QFileDialog>
#include <QSvgRenderer>
MySvg::MySvg()
{
m_effect = new QGraphicsColorizeEffect();
m_effect->setColor(Qt::red);
setGraphicsEffect(m_effect);
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
QString filename = QFileDialog::getOpenFileName(0, tr("Open Svg File"),
QString(), tr("Svg files (*.svg *.svgz)"));
setSharedRenderer(new QSvgRenderer(filename));
}
MySvg::~MySvg()
{
delete renderer();
delete m_effect;
}
void MySvg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QStyleOptionGraphicsItem opt(*option);
opt.state = QStyle::State_None;
QGraphicsSvgItem::paint(painter, &opt, widget);
//m_effect->setEnabled(false); // no effect though seemed logical
QRectF rectangle = boundingRect();
if (option->state & (QStyle::State_Selected))
drawSelectionRectangle(painter, option, rectangle);
//m_effect->setEnabled(true);
}
void MySvg::drawSelectionRectangle(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRectF &rectangle)
{
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine));
painter->setBrush(QColor(255, 188, 0, 50));
painter->drawRect(rectangle);
}
文件main.cpp
#include <QApplication>
#include <QGraphicsView>
#include "mysvg.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene s;
QGraphicsView view;
view.setScene(&s);
s.setSceneRect(-50, -50, 500, 650);
view.show();
MySvg* svg = new MySvg();
s.addItem(svg);
return app.exec();
}
文件mysvg.pro
QT += core gui svg
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = mysvg
TEMPLATE = app
SOURCES += main.cpp \
mysvg.cpp
HEADERS += mysvg.h
我考虑过将QGraphicsSvgItem
作为MySvg
项目的私人成员 - 但MySvg
项目必须做很多其他事情,我不知道如何处理渲染器(谁拥有它......) - 如果我能弄清楚如何使QGraphicsSvgItem
子类成为MySvg
类的成员,我可以将颜色应用于成员并对MySvg
项目执行所有其他操作......
请帮我找一种方法将颜色应用到svg,而不是项目的其他绘图部分。
编辑 - 我试图将一个成员项添加到该类并将该颜色效果应用于该成员 - 但它根本不应用着色效果... svg加载所有原始颜色。
以下是包含成员项目的代码:
new mysvg.h
class SvgMember : public QGraphicsSvgItem
{
public:
SvgMember (const QByteArray &content, const QColor& c);
~SvgMember ();
private:
QGraphicsColorizeEffect* m_effect;
};
class MySvg : public QGraphicsItem
{
public:
MySvg();
~MySvg();
virtual void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget = NULL);
virtual QRectF boundingRect() const;
virtual QPainterPath shape() const;
private:
void drawSelectionRectangle(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRectF& rectangle);
SvgMember * m_member;
};
new mysvg.cpp
MySvg::MySvg()
{
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
QString filename = QFileDialog::getOpenFileName(0, QObject::tr("Open Svg File"),
QString(), QObject::tr("Svg files (*.svg *.svgz)"));
QFile f(filename);
f.open(QFile::ReadOnly | QFile::Text);
QByteArray svgContents = f.readAll();
f.close();
m_member = new SvgMember (svgContents, Qt::red);
}
MySvg::~MySvg()
{
delete m_member;
}
void MySvg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QStyleOptionGraphicsItem opt(*option);
opt.state = QStyle::State_None;
m_member->paint(painter, &opt, widget);
QRectF rectangle = boundingRect();
if (option->state & (QStyle::State_Selected))
drawSelectionRectangle(painter, option, rectangle);
}
/*! \brief reimplemented to use member rectangle */
QRectF MySvg::boundingRect() const
{
return m_member->boundingRect();
}
/*! \brief reimplemented to use member shape */
QPainterPath MySvg::shape() const
{
return m_member->shape();
}
void MySvg::drawSelectionRectangle(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRectF &rectangle)
{
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine));
painter->setBrush(QColor(255, 188, 0, 50));
painter->drawRect(rectangle);
}
SvgMember ::SvgMember (const QByteArray &content, const QColor &c)
{
m_effect = new QGraphicsColorizeEffect();
setGraphicsEffect(m_effect);
m_effect->setColor(c);
setSharedRenderer(new QSvgRenderer(content));
}
SvgMember ::~SvgMember ()
{
delete renderer();
delete m_effect;
}
如何将着色效果应用于svg - 而不是选择矩形?
答案 0 :(得分:1)
效果是基类QGraphicsItem
的一个特征。它适用于整个图形项及其所有子项。因此,项目内部绘制的所有内容都会受其影响。
选择矩形应绘制在SVG项目对象之外。
可以通过将QGraphicsSvgItem
括在复合类QGraphicsItemGroup
中来实现。
当QGraphicsItem
添加到QGraphicsItemGroup
时,它会被重新定位。因此,在销毁组对象时销毁该项。因此,不需要手动删除它。 QGraphicsItem
取得效果的所有权,因此不需要删除效果对象。
以下课程MyGraphicsItemGroup
按预期工作。
实施&#34; mygraphicsitemgroup.cpp&#34;
#include "mygraphicsitemgroup.h"
#include <QGraphicsColorizeEffect>
#include <QGraphicsSvgItem>
#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <QFileDialog>
MyGraphicsItemGroup::MyGraphicsItemGroup()
{
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
QString filename = QFileDialog::getOpenFileName(0,
QObject::tr("Open Svg File"), QString(),
QObject::tr("Svg files (*.svg *.svgz)"));
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect();
effect->setColor(Qt::red);
QGraphicsSvgItem *svg = new QGraphicsSvgItem(filename);
svg->setGraphicsEffect(effect);
addToGroup(svg);
}
void MyGraphicsItemGroup::paint(QPainter* painter,
const QStyleOptionGraphicsItem* option, QWidget* widget)
{
QStyleOptionGraphicsItem opt(*option);
opt.state = QStyle::State_None;
QGraphicsItemGroup::paint(painter, &opt, widget);
QRectF rectangle = boundingRect();
if (option->state & QStyle::State_Selected)
drawSelectionRectangle(painter, option, rectangle);
}
void MyGraphicsItemGroup::drawSelectionRectangle(QPainter *painter,
const QStyleOptionGraphicsItem *option, const QRectF &rectangle)
{
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine));
painter->setBrush(QColor(255, 188, 0, 50));
painter->drawRect(rectangle);
}
Header&#34; mygraphicsitemgroup.h&#34;
#ifndef MYGRAPHICSITEMGROUP_H
#define MYGRAPHICSITEMGROUP_H
#include <QGraphicsItemGroup>
class MyGraphicsItemGroup : public QGraphicsItemGroup
{
public:
MyGraphicsItemGroup();
virtual void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option, QWidget* widget);
void drawSelectionRectangle(QPainter *painter,
const QStyleOptionGraphicsItem *option, const QRectF &rectangle);
};
#endif // MYGRAPHICSITEMGROUP_H