Using / Creating a cutom paintEngine
-
Goodmorning everyone!
I've been sitting over this problem for quite a while now:
I need to create a custom GraphicsItem. A working solution already exists, but its internal structure is going to change in the close future. In order to unit test its paint()-function I want to create my own GraphicsView using a custom PaintEngine (both only inside the test), which will gather information about what it has painted. If it works, I will reuse the painter and the engine for other tests, so it won't be too much of a waste of time finding a solution.
The problem is: it seems like my GraphicsView doesn't use my PaintEngine! For now it should only print a text to the console (especially in the drawRects function), but the debugger indicates that it never even enters any function besides the constructor...
I'm not exactly sure if I created and use everything correctly, since it was hard finding information on this topic...I'll post a compilable example of what I did so far:
@
#include <iostream>
#include <QApplication>
#include <QPainter>
#include <QPaintDevice>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMainWindow>
#include <QPaintEngine>
#include <QGraphicsItem>
#include <QWidget>
#include <QVector>class MyItem : public QGraphicsItem
{
private:
QVector<QRectF> _rectangles;
public:
MyItem(QGraphicsScene* scene, QGraphicsItem* parent = 0) : QGraphicsItem(parent, scene)
{
_rectangles.push_back(QRectF(0, 0, 100, 100));
_rectangles.push_back(QRectF(-50, -50, 100, 100));
}~MyItem()
{
}QRectF boundingRect() const
{
return QRectF();
}void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0)
{
painter->drawRects(_rectangles);
}
};class MyPaintEngine : public QPaintEngine
{
public:
MyPaintEngine(QPaintDevice* device)
{
std::cout << "Engine Konstruktor\n";
}bool hasFeature(PaintEngineFeatures feature) const
{
if( feature == QPaintEngine::PaintOutsidePaintEvent)
{
return true;
}
return QPaintEngine::hasFeature(feature);
}void drawPixmap(const QRectF &,const QPixmap &,const QRectF &)
{
}bool begin (QPaintDevice * pdev)
{
std::cout << "begin() Engine\n";
setActive(true);
setPaintDevice(pdev);
return true;
}bool end ()
{
setActive(false);
return true;
}void updateState ( const QPaintEngineState & state )
{
}Type type () const
{
return QPaintEngine::User;
}
void drawRects (const QRectF * rects, int rectCount)
{
std::cout << "drawRects Funktion\n";
}
};class MyView : public QGraphicsView
{
private:
MyPaintEngine* _engine;
public:
MyView(QGraphicsScene *scene, QWidget *parent = 0) : QGraphicsView(scene, parent)
{
std::cout << "MyView Konstruktor\n";
_engine = new MyPaintEngine(this);
}QPaintEngine* paintEngine() const
{
std::cout << "Return Custom Engine\n";
return _engine;
}
};int main(int argc, char *argv[])
{
QApplication application(argc, argv);QGraphicsScene scene;
MyView view(&scene);
MyItem item(&scene);
view.show();
view.setGeometry(300, 300, 300, 300);return application.exec();
}
@
*** Oooops, the example looked shorter in the IDE... and plaese have mercy on me for my coding style, I know it's an ugly example ;-)