Reputation: 484
I am trying to render a subclassed QGraphicsObject, called Part, to a QGraphicsScene.
class Part : public QGraphicsObject {
Q_OBJECT;
public:
Part(std::map<std::string, std::string> data, QString part_svg_file);
void setSharedRenderer(QSvgRenderer *r);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QRectF boundingRect() const;
QGraphicsSvgItem* GetPartSvg();
QGraphicsSvgItem* GetTerminalsSvg();
private:
QGraphicsSvgItem *m_part_graphic;
QGraphicsSvgItem *m_terminal_graphic;
QSvgRenderer *m_svg_renderer;
QDialog *m_dialog;
std::map<std::string, std::string> m_data;
}; // class Part
This part contains two QGraphicsSvgItems. These two items are generated from the same SVG file upon construction of Part:
Part::Part(std::map<std::string, std::string> data, QString part_svg_file)
: m_terminal_graphic(new QGraphicsSvgItem(part_svg_file)),
m_part_graphic(new QGraphicsSvgItem(part_svg_file)),
m_svg_renderer(new QSvgRenderer())
{
m_part_graphic->setElementId("part");
m_terminal_graphic->setElementId("terminals");
}
There are two items as the SVG contains two groups, one called "part" and the other called "terminals"
I want these two items to be rendered to the QGraphicsScene, I have them in seperate QGraphicsSvgItems to allow me to turn the "terminals" group off in certain instances.
I though the proper way to do this was to render them in the paint event of my parent QGraphicsObject class. However, this is not working.
void Part::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
painter->setPen(Qt::PenStyle::SolidLine);
painter->setPen(Qt::black);
painter->setBrush(Qt::SolidPattern);
painter->setBrush(Qt::black);
painter->drawRect(0, 0, 25, 25);
m_part_graphic->setSharedRenderer(m_svg_renderer);
m_svg_renderer->render(painter);
}
I am drawing a simple rectangle to ensure that the paint event is getting called properly. The rectangle is being drawn on the scene in the appropriate place. The QGraphicsSvgItem, m_part_graphic, is not being drawn on the scene. What is the proper way to do this?
To recap, I want to draw two different QGraphicsSvgItems onto a QGraphicsScene as part of a larger class called "Part".
UPDATE
At the suggestion below, I started a new project and began with a basic graphicsview render of an SVG.
m_svg_item = new QGraphicsSvgItem("C:\\Code\\QT_Testing\\QT_Testing\\Resources\\bubbles.svg");
m_svg_item->setFlags(QGraphicsItem::ItemClipsToShape);
m_svg_item->setZValue(0);
s->addItem(m_svg_item);
If I remove the s->addItem(m_svg_item);
line, it compiles and runs fine. Obviously doesn't draw the SVG at that point either.
It is crashing and giving me the follwing: (MS Visual Studio Pro 2012)
Output Window:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 3f0730. Receiver '' (of type 'QGraphicsSvgItem') was created in thread 3ff0a0", file kernel\qcoreapplication.cpp, line 535 Debug Error!
Program: C:\QT\4.8.5\bin\QtCored4.dll Module: 4.8.5 File: global\qglobal.cpp Line: 2303
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 3f0730. Receiver '' (of type 'QGraphicsSvgItem') was created in thread 3ff0a0", file kernel\qcoreapplication.cpp, line 535
And the Call Stack:
msvcr110d.dll!_CrtIsValidHeapPointer(const void * pUserData=0x004a76b0) Line 2036 C++
msvcr110d.dll!_free_dbg_nolock(void * pUserData=0x004a76b0, int nBlockUse=0) Line 1322 C++
msvcr110d.dll!_free_dbg(void * pUserData=0x004a76b0, int nBlockUse=0) Line 1265 C++
msvcr110d.dll!operator delete(void * pUserData=0x004a76b0) Line 54 C++
QtGuid4.dll!QGradientBrushData::`scalar deleting destructor'(unsigned int) C++
QtGuid4.dll!QBrushDataPointerDeleter::deleteData(QBrushData * d=0x004a76b0) Line 247 C++
QtGuid4.dll!QBrushDataPointerDeleter::cleanup(QBrushData * d=0x004a76b0) Line 257 C++
QtGuid4.dll!QScopedPointer<QBrushData,QBrushDataPointerDeleter>::reset(QBrushData * other=0x00432f38) Line 149 C++
QtGuid4.dll!QBrush::operator=(const QBrush & b={...}) Line 636 C++
main.cpp:
#include "qt_testing.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QT_Testing w;
w.show();
return a.exec();
}
qt_testing.cpp:
#include "qt_testing.h"
#include "svgview.h"
#include <QtGui>
QT_Testing::QT_Testing(QWidget *parent)
: QMainWindow(parent),
m_view(new SvgView)
{
setCentralWidget(m_view);
setWindowTitle("SVG Viewer");
}
QT_Testing::~QT_Testing()
{
}
void QT_Testing::openFile(const QString &path) {
QString file_name;
}
void QT_Testing::setRenderer(QAction *action) {
}
svgview.cpp:
#include "svgview.h"
#include <qfile.h>
#include <QtGui>
#include <QtSvg\qgraphicssvgitem.h>
SvgView::SvgView(QWidget *parent)
: QGraphicsView(parent)
, m_renderer(Native)
, m_svg_item(0)
, m_background_item(0)
, m_outline_item(0)
{
setScene(new QGraphicsScene(this));
setTransformationAnchor(AnchorUnderMouse);
setDragMode(ScrollHandDrag);
setViewportUpdateMode(FullViewportUpdate);
QPixmap tile_pixmap(64, 64);
tile_pixmap.fill(Qt::white);
QPainter tile_painter(&tile_pixmap);
QColor color(220, 220, 220);
tile_painter.fillRect(0, 0, 32, 32, color);
tile_painter.fillRect(32, 32, 32,32, color);
tile_painter.end();
setBackgroundBrush(tile_pixmap);
QGraphicsScene *s = scene();
s->clear();
resetTransform();
m_svg_item = new QGraphicsSvgItem("C:\\Code\\QT_Testing\\QT_Testing\\Resources\\bubbles.svg");
m_svg_item->setFlags(QGraphicsItem::ItemClipsToShape);
m_svg_item->setCacheMode(QGraphicsItem::NoCache);
m_svg_item->setZValue(0);
/*m_background_item = new QGraphicsRectItem(QRect(0, 0, 300, 300));
m_background_item->setBrush(Qt::white);
m_background_item->setPen(Qt::NoPen);
m_background_item->setVisible(true);
m_background_item->setZValue(-1);*/
//s->addItem(m_background_item);
s->addItem(m_svg_item);
s->addEllipse(150, 150, 100, 100, QPen(Qt::black, Qt::PenStyle::SolidLine));
s->setSceneRect(-10, -10, 320, 320);
}
void SvgView::drawBackground(QPainter *p, const QRectF &) {
p->save();
p->resetTransform();
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
p->restore();
}
void SvgView::paintEvent(QPaintEvent *event) {
QGraphicsView::paintEvent(event);
}
void SvgView::wheelEvent(QWheelEvent *event) {
qreal factor = qPow(1.2, event->delta() / 240.0);
scale(factor, factor);
event->accept();
}
Upvotes: 3
Views: 2966
Reputation: 484
As much as I hate this answer, here is what I did to fix this. Since QT 4.8.5 is not officially supported in Visual Studio 2012, I wiped my QT install completely and installed QT 5.2.1. After fixing a few include paths, the above code compiled and ran just fine. The SVGs are now drawing as expected. Thank you Kuba Ober for your help. It is much appreciated.
Upvotes: 1