Restrict QGraphicsLineItem movement



  • 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* ?



  • 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);
    @



  • 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);
    

    }
    @



  • 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 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.