"int QGraphicsItem::type() const [virtual]" method of QGraphicsItem class enables the clients to check for the type of the custom QGraphicsItem. Each unique QGraphicsItem instance returns a unique integer value from the type() method.
For custom QGraphicsItem classes it is also required to implement
"QRectF QGraphicsItem::boundingRect(void) const"
and
"void QGraphicsItem::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)"
methods additionally.
"CustomGraphicsItems.h" file contains TextItem and LineItem class definitions.
#include <QGraphicsItem> class TextItem : public QGraphicsItem { public: enum { Type = UserType + 1 }; int type() const { return Type; } TextItem(QGraphicsItem* parent = 0 ); ~TextItem(); protected: void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0 ); QRectF boundingRect() const; }; class LineItem : public QGraphicsItem { public: enum { Type = UserType + 2 }; int type() const { return Type; } LineItem(QGraphicsItem* parent = 0); ~LineItem(); protected: void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0 ); QRectF boundingRect() const; };
Each custom QGraphicsItem instance is uniquely identified in the enum .
"CustomGraphicsItems.cpp" file contains implementation for TextItem and LineItem classes.
#include "CustomGraphicsItems.h" TextItem ::TextItem(QGraphicsItem* parent) : QGraphicsItem(parent) {} TextItem::~TextItem() {} QRectF TextItem::boundingRect() const { return QRectF(); } void TextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {} LineItem ::LineItem(QGraphicsItem* parent) : QGraphicsItem(parent) {} LineItem::~LineItem() {} void LineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {} QRectF LineItem::boundingRect() const { return QRectF(); }
"CustomScene.h" file contains derived QGraphicsScene class.
#include <QGraphicsScene> class CustomScene : public QGraphicsScene { public : CustomScene(QObject * parent = 0); ~CustomScene(); };
"CustomScene.cpp" file contains implementation for derived QGraphicsScene class.
#include "CustomScene.h" CustomScene::CustomScene(QObject * parent) : QGraphicsScene(parent) {} CustomScene::~CustomScene() {}
"CustomMainWindow.h" file contains definition for derived QMainWindow class.
#include <QMainWindow> class CustomScene; class CustomMainWindow : public QMainWindow { public: CustomMainWindow(QWidget *parent = 0); ~CustomMainWindow(); void displayItemType(); void addItemsToGraphicsScene(); private: CustomScene* customScene; };
"CustomMainWindow.cpp" file contains implementation for derived QMainWindow class.
#include <QDebug> #include "CustomMainWindow.h" #include "CustomScene.h" #include "CustomGraphicsItems.h" CustomMainWindow :: CustomMainWindow(QWidget *parent) { customScene = new CustomScene(this); addItemsToGraphicsScene(); displayItemType(); } void CustomMainWindow :: addItemsToGraphicsScene() { TextItem* textItem = new TextItem(); customScene->addItem(textItem); LineItem* lineItem = new LineItem(); customScene->addItem(lineItem); } void CustomMainWindow :: displayItemType() { QList<QGraphicsItem*> itemList = customScene->items(); for(int i=0; i<itemList.size(); i++){ if(itemList[i]->type() == TextItem::Type ) qDebug() << "I am TextItem"; else if(itemList[i]->type() == LineItem::Type) qDebug() << "I am LineItem"; } } CustomMainWindow :: ~CustomMainWindow() {}
"Main.cpp" file contains an instance of CustomMainWindow class. CustomScene is the container for the TextItem and LineItem instances and it is initialized in the CustomMainWindow constructor. addItemsToGraphicsScene() method of CustomMainWindow adds TextItem and LineItem instances to the CustomScene.
Types of custom QGraphicsItem instances are checked in displayItemType() method of CustomMainWindow class.
#include <QApplication> #include "CustomMainWindow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); CustomMainWindow* cm = new CustomMainWindow; delete cm; return app.exec(); }
Tested with MSVS2008 and QT4.6.0
No comments:
Post a Comment