Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Strange casting error for qgraphicsview?
Forum Updated to NodeBB v4.3 + New Features

Strange casting error for qgraphicsview?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndreAhmed
    wrote on last edited by
    #1

    I have an object that inherits from QGraphicsitem, and it has an enum, when I write that code, it gives error for enum casting problem. Why that happens?

    The problem is here :
    @
    Node *startItem =
    qgraphicsitem_cast<Node *>(startItems.first());
    Node *endItem =
    qgraphicsitem_cast<Node *>(endItems.first());
    @
    @
    if (mLink != 0 && mMode == Insert_Line) {
    QList<QGraphicsItem *> startItems = items(mLink->line().p1());
    if (startItems.count() && startItems.first() == mLink)
    startItems.removeFirst();
    QList<QGraphicsItem *> endItems = items(mLink->line().p2());
    if (endItems.count() && endItems.first() == mLink)
    endItems.removeFirst();

        removeItem(mLink);
        delete mLink;
    
        // if two nodes are connected by the line
        if (startItems.count() > 0 && endItems.count() > 0 &&
                startItems.first()&& endItems.first() &&
                startItems.first() != endItems.first()) {
            Node *startItem =
                    qgraphicsitem_cast<Node *>(startItems.first());
            Node *endItem =
                    qgraphicsitem_cast<Node *>(endItems.first());
    
            // add a line
            Link *arrow = new Link(startItem,endItem);
            arrow->trackNodes();
            addItem(arrow);
            arrow->updatePosition();
    
        }
    

    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You should also post the error message.
      Would be good to know the OS and compiler as well.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AndreAhmed
        wrote on last edited by
        #3

        Thanks for reply.

        Windows7, QTCreator, Mingw.

        The error is

        casting error <Node*>

        and it is in that line:

        Node *startItem =
        qgraphicsitem_cast<Node *>(startItems.first());
        Node *endItem =
        qgraphicsitem_cast<Node *>(endItems.first());

        my node class is :
        @
        class Node : public QGraphicsItem
        {
        Q_DECLARE_TR_FUNCTIONS(Node)
        public:
        enum Type { Input, Output};

        public:

        Node(QGraphicsItem *parent = 0, const Type& type = Input);
        ~Node();
        
        void setText(const QString &text);
        

        };
        @

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on last edited by
          #4

          quote from docs:

          bq. To make this function work correctly with custom items, reimplement the "type()":http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#type function for each custom QGraphicsItem subclass.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AndreAhmed
            wrote on last edited by
            #5

            I just need a user defined enumeration, why should I reimplement type function?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

              @template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
              {
              return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
              || (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
              }
              @

              As you see item->type() is used in qgraphicsitem_cast function

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved