Results 1 to 5 of 5

Thread: QGraphicsItem in a grid

  1. #1
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem in a grid

    I would like to know how can I add my custom QGraphicsItem in a grid.

    custom-tem.cpp
    Qt Code:
    1. #include "custom-item.h"
    2.  
    3. CustomItem::CustomItem(QGraphicsItem *parent)
    4. : QGraphicsItem(parent)
    5. {
    6. /* drawPolygon(...)*/
    7. polygon << QPointF(55, 180) << QPointF(35, 120)
    8. << QPointF(75, 40) << QPointF(115, 20)
    9. << QPointF(195, 80) << QPointF(215, 160)
    10. << QPointF(115, 220);
    11. }
    12. QRectF CustomItem::boundingRect() const
    13. {
    14. return QRectF(0, 0, 250, 250);
    15. }
    16. void CustomItem::paint(QPainter *painter,
    17. const QStyleOptionGraphicsItem *option,
    18. QWidget *widget)
    19. {
    20. painter->setRenderHint(QPainter::Antialiasing);
    21. painter->setBrush(Qt::blue);
    22. }
    23.  
    24. void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    25. {
    26. qDebug() << "Mouse button clicked at position: "
    27. << event->pos();
    28. }
    29.  
    30. void CustomItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    31. {
    32. qDebug() << "Mouse button released at position: "
    33. << event->pos();
    34. }
    To copy to clipboard, switch view to plain text mode 

    custom-item.h
    Qt Code:
    1. #ifndef CUSTOM_ITEM_H
    2. #define CUSTOM_ITEM_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QPointF>
    6. #include <QRectF>
    7. #include <QPolygonF>
    8. #include <QPainter>
    9. #include <QPainterPath>
    10. #include <QWidget>
    11. #include <QGraphicsSceneMouseEvent>
    12. #include <QDebug>
    13.  
    14.  
    15.  
    16. class CustomItem : public QGraphicsItem
    17. {
    18. public:
    19. CustomItem(QGraphicsItem *parent = 0);
    20.  
    21. QRectF boundingRect() const;
    22. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    23.  
    24. private:
    25. QPolygonF polygon;
    26. QPixmap pixmap;
    27.  
    28. protected:
    29. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    30. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    31.  
    32. };
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4.  
    5. #include "custom-item.h"
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QGraphicsScene scene(0, 0, 300, 300);
    12.  
    13. CustomItem *item = new CustomItem();
    14. item->setPos(24, 24);
    15.  
    16. CustomItem *item2 = new CustomItem();
    17. item2->setPos(80, 24);
    18.  
    19. scene.addItem(item);
    20. scene.addItem(item2);
    21.  
    22. QGraphicsView view(&scene);
    23. view.setRenderHints( QPainter::Antialiasing );
    24.  
    25. view.show();
    26.  
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem in a grid

    Quote Originally Posted by GuL View Post

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsItem in a grid


  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem in a grid

    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 .
    So if anyone can help meanwhile
    Last edited by aamer4yu; 8th June 2010 at 12:06.

  5. #5
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem in a grid

    Quote Originally Posted by aamer4yu View Post
    So how do we translate flickr.qml into pure code ?
    Did you do it?

Similar Threads

  1. Replies: 3
    Last Post: 15th February 2010, 21:07
  2. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 15:37
  3. Replies: 2
    Last Post: 28th June 2008, 16:31
  4. Snap-to grid on QGraphicsItem
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 28th April 2008, 16:22
  5. Grid layout
    By nupul in forum Qt Programming
    Replies: 7
    Last Post: 21st April 2006, 21:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.