Results 1 to 11 of 11

Thread: QGraphicsItem subclass compilation error

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem subclass compilation error

    Hi,

    I have a class that inherit from QGrpahicsEllipseItem to let it emit a signal when it is selected

    Qt Code:
    1. #ifndef QDEFECTITEM_H
    2. #define QDEFECTITEM_H
    3.  
    4. #include <QGraphicsEllipseItem>
    5.  
    6. class QDefectItem : public QGraphicsEllipseItem
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. QDefectItem();
    12. ~QDefectItem();
    13.  
    14. protected:
    15. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    16.  
    17. signals:
    18. void itemSelected(QGraphicsItem*);
    19. };
    20.  
    21. #endif // QDEFECTITEM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "QDefectItem.h"
    2.  
    3. QDefectItem::QDefectItem()
    4. {
    5.  
    6. }
    7.  
    8. QDefectItem::~QDefectItem()
    9. {
    10.  
    11. }
    12.  
    13. QVariant QDefectItem::itemChange(GraphicsItemChange change,const QVariant &value)
    14. {
    15. if (change == QGraphicsItem::ItemSelectedChange)
    16. emit itemSelected(this);
    17. return value;
    18. }
    To copy to clipboard, switch view to plain text mode 

    This simple code returns an error when tryies to compile the generated moc file.
    Missing something?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsItem subclass compilation error

    To make use of signal/slots the class should be derived from directly or indirectly from QObject. QGraphicsEllipseItem does not have QObject in it's inheritnace tree. One thing you can do is, multiple inheritance of QObject and QGraphicsEllipseItem
    Qt Code:
    1. class QDefectItem : public QObject, public QGraphicsEllipseItem
    2. {
    3. Q_OBJECT
    4. ...
    5. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    ^NyAw^ (30th January 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem subclass compilation error

    Hi,

    I was lloking the "diagramscene" example and didn't notice that QGraphicsTextItem inherits from QGraphicsObject that is needed to use signal/slots.

    Thanks,


    Added after 27 minutes:


    Now, I get "cannot create instance from an absract class"
    Last edited by ^NyAw^; 30th January 2013 at 11:12.
    Òscar Llarch i Galán

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsItem subclass compilation error

    Now, I get "cannot create instance from an absract class"
    What abstract class you are referring to?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem subclass compilation error

    Hi,

    Just when I call this:
    Qt Code:
    1. QDefectItem *item = new QDefectItem();
    To copy to clipboard, switch view to plain text mode 

    I also get an error when calling "isEnabled()"
    Qt Code:
    1. #ifndef QDEFECTITEM_H
    2. #define QDEFECTITEM_H
    3.  
    4. #include <QGraphicsEllipseItem>
    5.  
    6. class QDefectItem : public QGraphicsObject, public QGraphicsEllipseItem
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. QDefectItem();
    12. ~QDefectItem();
    13.  
    14. private slots:
    15. void enabledChg();
    16.  
    17. signals:
    18. void itemSelected(QDefectItem*);
    19. };
    20.  
    21. #endif // QDEFECTITEM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "QDefectItem.h"
    2.  
    3. QDefectItem::QDefectItem()
    4. {
    5. //connect(this,SIGNAL(enabledChanged()),this,SLOT(enabledChg()));
    6. }
    7.  
    8. QDefectItem::~QDefectItem()
    9. {
    10.  
    11. }
    12.  
    13. void QDefectItem::enabledChg()
    14. {
    15. //here there is another error
    16. if (isEnabled()) //error C2358:"ambiguous acces to isEnabled()" and error C3861:"isEnabled identifier not found"
    17. emit itemSelected(this);
    18. }
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem subclass compilation error

    Inherit from QObject instead of QGraphicsObject. The latter is an abstract class anyway.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    ^NyAw^ (30th January 2013)

  9. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem subclass compilation error

    Hi,

    And now, how can I get a signal when the item is selected if it not inherit from QGraphicsObject?
    Tried to use "itemChange" but is not called when I click an item.

    Thanks,
    Òscar Llarch i Galán

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem subclass compilation error

    Quote Originally Posted by ^NyAw^ View Post
    And now, how can I get a signal when the item is selected if it not inherit from QGraphicsObject?
    Exactly the same way you'd do it if it inherited from QGraphicsObject since QGraphicsObject doesn't provide any means to be notified when an item gets selected.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem subclass compilation error

    Hi,

    Just adding "setFlag(QGraphicsItem::ItemIsSelectable)" to the items.

    Thanks,
    Òscar Llarch i Galán

  12. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsItem subclass compilation error

    Did you try this code from your first post?
    Qt Code:
    1. QVariant QDefectItem::itemChange(GraphicsItemChange change,const QVariant &value)
    2. {
    3. if (change == QGraphicsItem::ItemSelectedChange)
    4. emit itemSelected(this);
    5. return value;
    6. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  13. #11
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem subclass compilation error

    Hi,

    Yes, but I forget to set the item selectable.

    Thanks,
    Òscar Llarch i Galán

Similar Threads

  1. QGraphicsItem subclass and accessing custom properties
    By been_1990 in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 01:48
  2. Qt 4.6.2. compilation error
    By b1 in forum Installation and Deployment
    Replies: 3
    Last Post: 13th June 2010, 08:22
  3. Strange compile error in subclass
    By space_otter in forum General Programming
    Replies: 4
    Last Post: 3rd March 2010, 23:26
  4. QGraphicsItem subclass access to QGraphicsView size
    By rubenvb in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2010, 21:36

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.