PDA

View Full Version : QGraphicsItem in a grid



GuL
18th May 2010, 16:41
I would like to know how can I add my custom QGraphicsItem in a grid.

custom-tem.cpp


#include "custom-item.h"

CustomItem::CustomItem(QGraphicsItem *parent)
: QGraphicsItem(parent)
{
/* drawPolygon(...)*/
polygon << QPointF(55, 180) << QPointF(35, 120)
<< QPointF(75, 40) << QPointF(115, 20)
<< QPointF(195, 80) << QPointF(215, 160)
<< QPointF(115, 220);
}
QRectF CustomItem::boundingRect() const
{
return QRectF(0, 0, 250, 250);
}
void CustomItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(Qt::blue);
}

void CustomItem::mousePressEvent(QGraphicsSceneMouseEve nt *event)
{
qDebug() << "Mouse button clicked at position: "
<< event->pos();
}

void CustomItem::mouseReleaseEvent(QGraphicsSceneMouseE vent *event)
{
qDebug() << "Mouse button released at position: "
<< event->pos();
}


custom-item.h


#ifndef CUSTOM_ITEM_H
#define CUSTOM_ITEM_H

#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>
#include <QPolygonF>
#include <QPainter>
#include <QPainterPath>
#include <QWidget>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>



class CustomItem : public QGraphicsItem
{
public:
CustomItem(QGraphicsItem *parent = 0);

QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

private:
QPolygonF polygon;
QPixmap pixmap;
QPainterPath path;

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

};

#endif


main.cpp


#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>

#include "custom-item.h"

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QGraphicsScene scene(0, 0, 300, 300);

CustomItem *item = new CustomItem();
item->setPos(24, 24);

CustomItem *item2 = new CustomItem();
item2->setPos(80, 24);

scene.addItem(item);
scene.addItem(item2);

QGraphicsView view(&scene);
view.setRenderHints( QPainter::Antialiasing );

view.show();

return app.exec();
}


I know it is possible to do it using the QGraphicsLayoutItem class. Can someone give me an example of how to do it?

GuL
19th May 2010, 15:28
I know it is possible to do it using the QGraphicsLayoutItem class. Can someone give me an example of how to do it?

Should I subclass my custom-item class or what?
I'm lost.

Any help would be appreciated.


Thanks

tbscope
19th May 2010, 15:33
http://doc.qt.nokia.com/4.6/qgraphicsgridlayout.html

Contains an example

aamer4yu
8th June 2010, 11:06
I had similar problem, and am thinking whats the best design in this case.
The example in QGraphicsGridLayout uses QGraphicsWidget. Now suppose if we want image thumbnails in a grid layout, we can use QGraphicsItem and place them manually.

To use QGraphicsGridLayout, we will need to make each item derived from QGraphicsWidget, have a holder graphicsWidget and set grid layout on this holder widget. Then we can add the thumbnails to this grid layout.
Also if your item was QGraphicsPixmapItem or some other QGraphicsItem derived class, how do we port that to layout ?

Isnt there a simpler way ? or we have to go the harder way ?


To say this other way, if we look at fliickr.qml example, it does similar to this question. It uses view / delegates too with the graphics view. So how do we translate flickr.qml into pure code ? Just started looking at it, need to understand qml first :D.
So if anyone can help meanwhile :rolleyes:

GuL
13th October 2010, 17:42
So how do we translate flickr.qml into pure code ?

Did you do it?