Results 1 to 5 of 5

Thread: Moving an item in QGraphicsScene

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving an item in QGraphicsScene

    Thanks thawkins, but I think it´s not exactly what I need.

    I have followed the mice colliding example and I´ve created a timer event:

    The .h file:

    Qt Code:
    1. #ifndef CHIP_H
    2. #define CHIP_H
    3.  
    4. #include <QtGui/QColor>
    5. #include <QtGui/QGraphicsItem>
    6. #include <QObject>
    7.  
    8. class Chip : public QGraphicsItem
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Chip(const QColor &color, int x, int y, const QString &tipo, const qreal &ancho, const qreal &alto, const QPixmap &pixmap);
    14.  
    15. QRectF boundingRect() const;
    16. QPainterPath shape() const;
    17. void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
    18.  
    19. protected:
    20. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    21. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    22. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    23. void timerEvent(QTimerEvent *event);
    24. [/B]
    25. private:
    26. int x, y;
    27. QColor color;
    28. QString tipo;
    29. QPixmap pixmap;
    30. qreal ancho;
    31. qreal alto;
    32. QList<QPointF> stuff;
    33. };
    To copy to clipboard, switch view to plain text mode 

    And in the .cpp file I have reimplemented the timer event:

    Qt Code:
    1. #include "chip.h"
    2. #include "loc.h"
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6.  
    7.  
    8. loc_position_t loc_pos;
    9.  
    10. Chip::Chip(const QColor &color, int x, int y, const QString &tipo, const qreal &ancho, const qreal &alto, const QPixmap &pixmap)
    11. {
    12. this->x = x;
    13. this->y = y;
    14. this->color = color;
    15. this->tipo = tipo;
    16. this->ancho = ancho;
    17. this->alto = alto;
    18. this->pixmap = pixmap;
    19.  
    20. setZValue(1);
    21.  
    22. setFlags(ItemIsSelectable);
    23. setAcceptsHoverEvents(true);
    24. startTimer(5000);
    25.  
    26. }
    27.  
    28. ...
    29.  
    30. void Chip::timerEvent(QTimerEvent *)
    31. {
    32. ...
    33. }
    To copy to clipboard, switch view to plain text mode 

    Now my problem is I get the error:

    Qt Code:
    1. :: === chip2, Debug ===
    2. chip.cpp:24: error: `startTimer' undeclared (first use this function)
    3.  
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Why is that? I have rerun qmake and the Q_OBJECT macro is the .h file. I cannot find why the error. Can anyone help?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Moving an item in QGraphicsScene

    QGraphicsItem is not a QObject. You would have to use multiple inheritance:
    Qt Code:
    1. class Chip : public QObject, public QGraphicsItem // QObject must be listed first
    2. {
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 
    Anyway, if you plan to construct any kind of animation, I suggest using QGraphicsItemAnimation.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    prosass (28th March 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving an item in QGraphicsScene

    Thanks jpn, that solved the problem!

Similar Threads

  1. QWidget item in QGraphicsScene
    By IUnknown in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2006, 00:05
  2. QTableWidget item checkable and combo?
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 07:12
  3. what item QCanvasItemList is holding..
    By Kapil in forum Newbie
    Replies: 17
    Last Post: 25th April 2006, 14:57
  4. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 12:25
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 01:34

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
  •  
Qt is a trademark of The Qt Company.