Results 1 to 8 of 8

Thread: qgraphicsitem_cast cannot convert seft-defined types

  1. #1
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qgraphicsitem_cast cannot convert seft-defined types

    I have a class inherits QGraphicsRectItem with type function
    Qt Code:
    1. int RectItem::type() const
    2. {
    3. return UserType + 1;
    4. }
    To copy to clipboard, switch view to plain text mode 
    But when I try to coverting a QGraphicsItem pointer to RectItem pointer, the qgraphicsitem_cast returns null.
    Qt Code:
    1. QList<QGraphicsItem *> items = scene()->selectedItems();
    2. for(int i=0; i<items.size(); i++){
    3. if(items[i]->type() == UserType + 1)
    4. RectItem*rectItem = qgraphicsitem_cast<RectItem*>(items[i]);
    To copy to clipboard, switch view to plain text mode 
    Here rectItem is null. However if I change the cast to dynamic_cast, the convertion will be successful.
    Qt Code:
    1. QList<QGraphicsItem *> items = scene()->selectedItems();
    2. for(int i=0; i<items.size(); i++){
    3. if(items[i]->type() == UserType + 1){
    4. RectItem*rectItem = dynamic_cast<RectItem*>(items[i]);
    To copy to clipboard, switch view to plain text mode 
    Here rectItem is the real RectItem selected.

    Anyone knows why qgraphicsitem_cast is not working?
    Thanks in advance.

  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_cast cannot convert seft-defined types

    Implement it this way
    Qt Code:
    1. class RectItem : public QGraphicsItem
    2. {
    3. ...
    4. public:
    5. ...
    6. enum { Type = UserType + 1 };
    7.  
    8. int type() const
    9. {
    10. return Type;
    11. }
    12. ...
    13. };
    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. #3
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qgraphicsitem_cast cannot convert seft-defined types

    I tried to use class enum and global enum, but they work the same as without using enum—— type checking returns true, but qgraphicsitem_cast returns null.

  4. #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_cast cannot convert seft-defined types

    Here is an example, it works perfectly, if it still does not work for you, please post a minimum compilable code

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class RectItem : public QGraphicsItem
    4. {
    5. public:
    6. explicit RectItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0)
    7. : QGraphicsItem(parent, scene) { }
    8.  
    9. enum { Type = UserType + 1 };
    10.  
    11. int type() const { return Type; }
    12.  
    13. QRectF boundingRect() const { return QRectF(); }
    14.  
    15. void paint(QPainter */*painter*/, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/ = 0) {}
    16. };
    17.  
    18. int main(int /*argc*/, char **/*argv*/)
    19. {
    20. RectItem rectItem;
    21. QGraphicsItem * item = &rectItem;
    22.  
    23. qDebug() << "1." << &rectItem;
    24. qDebug() << "2." << qgraphicsitem_cast<RectItem*>(item);
    25.  
    26. return 0;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    1. QGraphicsItem (this = 0x22feec , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags = ( ) )
    2. QGraphicsItem (this = 0x22feec , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags = ( ) )
    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.

  5. #5
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qgraphicsitem_cast cannot convert seft-defined types

    The code you gave is working for me, too. Maybe the reason is you are inheriting from QGraphicsItem but not QGraphicsRectItem.
    Here is the code not working.

    graphicsItem.h
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class RectItem : public QGraphicsRectItem
    4. {
    5. public:
    6. RectItem(QGraphicsItem * parent = 0);
    7. RectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0);
    8.  
    9. enum {RectType = QGraphicsRectItem::UserType + 1};
    10. int type() const;
    11. };
    To copy to clipboard, switch view to plain text mode 

    graphicsItem.cpp
    Qt Code:
    1. #include "graphicsItem.h"
    2.  
    3. RectItem::RectItem(QGraphicsItem * parent)
    4. : QGraphicsRectItem(parent){}
    5.  
    6. RectItem::RectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent)
    7. : QGraphicsRectItem(x, y, width, height, parent){}
    8.  
    9. int RectItem::type() const {return RectType; }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "graphicsItem.h"
    2. int main()
    3. {
    4. RectItem rectItem;
    5. QGraphicsItem * item = &rectItem;
    6.  
    7. if(qgraphicsitem_cast<RectItem*>(item) == NULL)
    8. qDebug() << "qgraphicsitem_cast Failed";
    9. else
    10. qDebug() << "qgraphicsitem_cast Success";
    11.  
    12. if(dynamic_cast<RectItem*>(item) == NULL)
    13. qDebug() << "dynamic_cast Failed";
    14. else
    15. qDebug() << "dynamic_cast Success";
    16.  
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    qgraphicsitem_cast Failed
    dynamic_cast Success

  6. #6
    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_cast cannot convert seft-defined types

    Ahh, I see your problem, In QGraphicsRectItem class type() function is no more virtual, so inherting from QGraphicsRectItem, and re-implementing type() function is a mear function override in derived class, and is no more polymorphic (virtual) function.

    There are two ways to solve, see which one will fit your requirement.

    1. Inherit directy from QGraphicsItem, and reimplement type(), and you know what to do then.
    2. Inherit from QGraphicsRectItem, and don't implement type(), and offcourse you can use only dynamic_cast<> (qgraphicsitem_cast will not work )
    Last edited by Santosh Reddy; 10th May 2013 at 15:47. Reason: spelling corrections
    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.

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

    Seishin (10th May 2013)

  8. #7
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qgraphicsitem_cast cannot convert seft-defined types

    Thanks a lot. Actually I think I can still implement type() to override the type() function of QGraphicsRectItem. This may help if I have multiple children classes like RectItem1, RectItem2, etc.

  9. #8
    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_cast cannot convert seft-defined types

    Quote Originally Posted by Santosh Reddy
    Ahh, I see your problem, In QGraphicsRectItem class type() function is no more virtual, so inherting from QGraphicsRectItem, and re-implementing type() function is a mear function override in derived class, and is no more polymorphic (virtual) function.
    This is not correct, I am sorry for the inconvenience caused.

    The real reason your code is not wokring is that the enum name is supposed to be "Type" (not "RectType"). Modify you code like this
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class RectItem : public QGraphicsRectItem
    4. {
    5. public:
    6. RectItem(QGraphicsItem * parent = 0);
    7. RectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0);
    8.  
    9. //enum {RectType = QGraphicsRectItem::UserType + 1};
    10. enum {Type = QGraphicsRectItem::UserType + 1};
    11. int type() const;
    12. };
    13.  
    14. RectItem::RectItem(QGraphicsItem * parent)
    15. : QGraphicsRectItem(parent){}
    16.  
    17. RectItem::RectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent)
    18. : QGraphicsRectItem(x, y, width, height, parent){}
    19.  
    20. //int RectItem::type() const {return RectType; }
    21. int RectItem::type() const {return Type; }
    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.

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

    Seishin (13th May 2013)

Similar Threads

  1. Problem with qgraphicsitem_Cast
    By kokeroulis in forum Newbie
    Replies: 13
    Last Post: 21st September 2010, 16:55
  2. Replies: 4
    Last Post: 10th December 2009, 16:37
  3. QGraphicsItemPrivate and qgraphicsitem_cast
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2009, 08:13
  4. qgraphicsitem_cast problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2008, 15:22
  5. regarding qgraphicsitem_cast
    By Gopala Krishna in forum Qt Programming
    Replies: 6
    Last Post: 23rd December 2006, 15:09

Tags for this Thread

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.