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. Restrict QGraphicsLineItem movement
Qt 6.11 is out! See what's new in the release blog

Restrict QGraphicsLineItem movement

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.8k 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
    Artemus
    wrote on last edited by
    #1

    Hallo,

    I want to restrict the movement of some QGraphicsLineItem and want to create a "snap to grid" behavior.

    I created a new class called "QGraphicsLineItemMember" wich inherits QGraphicsLineItem to modify the itemChange() function.
    The Problem is that with the new class i cant use @scene->addLine()@

    to create the line at the specified scene.

    Is it posible to cast QGraphicsLineItemMember* to QGraphicsLineItem* ?

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

      If QGraphicsLineItemMember is derived from QGraphicsLineItem, then you do not need to cast. QGraphicsLineItemMember is already an QGraphicsLineItem object. For example in any QGraphicsLineItemMember function member you can use
      @
      scene()->addLine(this);
      @

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

        but i get this error when i replace my lineitems with the new class:
        @error: invalid conversion from 'QGraphicsLineItem*' to 'QGraphicsLineItemMember*'@

        at this line:
        @pq_line1 = pq_scn->addLine(0,0,0,0,pen);@

        my sourcecode:
        @
        class QGraphicsLineItemMember : public QGraphicsLineItem
        {
        public:
        int test;
        };

        class CL_STATE
        {
        public:
        CL_STATE( QGraphicsScene* pq_scene );
        void setPoint1(qreal x, qreal y);
        void setPoint2(qreal x, qreal y);
        void setNorth(qreal n);
        void setSouth(qreal s);
        void setEast(qreal e);
        void setWest(qreal w);
        void redraw(void);

        signals:

        public slots:

        private:
        qreal x1,y1,x2,y2;
        QGraphicsScene* pq_scn;
        QGraphicsLineItem* pq_line1; //works
        QGraphicsLineItem* pq_line2;
        QGraphicsLineItemMember* pq_line3; //does not work
        QGraphicsLineItemMember* pq_line4;
        };
        @

        @
        CL_STATE::CL_STATE( QGraphicsScene* pq_scene ):
        pq_scn(pq_scene)
        {
        QPen pen;
        pen.setWidth(2);

        //add lines
        pq_line1 = pq_scn->addLine(0,0,0,0,pen);
        pq_line2 = pq_scn->addLine(0,0,0,0,pen);
        pq_line3 = pq_scn->addLine(0,0,0,0,pen);
        pq_line4 = pq_scn->addLine(0,0,0,0,pen);
        
        //make movable
        pq_line1->setFlag(QGraphicsItem::ItemIsMovable);
        pq_line2->setFlag(QGraphicsItem::ItemIsMovable);
        pq_line3->setFlag(QGraphicsItem::ItemIsMovable);
        pq_line4->setFlag(QGraphicsItem::ItemIsMovable);
        pq_line1->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
        pq_line2->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
        pq_line3->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
        pq_line4->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
        
        //set move cursor
        pq_line1->setCursor(Qt::SizeAllCursor);
        pq_line2->setCursor(Qt::SizeAllCursor);
        pq_line3->setCursor(Qt::SizeAllCursor);
        pq_line4->setCursor(Qt::SizeAllCursor);
        

        }
        @

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

          If you want to add QGraphicsLineItemMember object to scene you need to use "QGraphicsScene::addItem":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicsscene.html#addItem
          @
          QGraphicsLineItemMember* lineItemMember = new QGraphicsLineItemMember();
          pq_scn->addItem(lineItemMember);

          @

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

            [quote author="cincirin" date="1327748569"]If you want to add QGraphicsLineItemMember object to scene you need to use "QGraphicsScene::addItem":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicsscene.html#addItem
            @
            QGraphicsLineItemMember* lineItemMember = new QGraphicsLineItemMember();
            pq_scn->addItem(lineItemMember);

            @[/quote]
            works perfect, thank you!

            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