January 27, 2012

Artemus Artemus
Lab Rat
12 posts

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

  1. scene->addLine()

to create the line at the specified scene.

Is it posible to cast QGraphicsLineItemMember* to QGraphicsLineItem* ?

4 replies

January 28, 2012

cincirin cincirin
Robot Herder
270 posts

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

  1. scene()->addLine(this);

January 28, 2012

Artemus Artemus
Lab Rat
12 posts

but i get this error when i replace my lineitems with the new class:

  1. error: invalid conversion from 'QGraphicsLineItem*' to 'QGraphicsLineItemMember*'

at this line:

  1. pq_line1 = pq_scn->addLine(0,0,0,0,pen);

my sourcecode:

  1. class QGraphicsLineItemMember : public QGraphicsLineItem
  2. {
  3. public:
  4.     int test;
  5. };
  6.  
  7. class CL_STATE
  8. {
  9. public:
  10.     CL_STATE( QGraphicsScene* pq_scene );
  11.     void setPoint1(qreal x, qreal y);
  12.     void setPoint2(qreal x, qreal y);
  13.     void setNorth(qreal n);
  14.     void setSouth(qreal s);
  15.     void setEast(qreal e);
  16.     void setWest(qreal w);
  17.     void redraw(void);
  18.  
  19. signals:
  20.  
  21. public slots:
  22.  
  23. private:
  24.     qreal x1,y1,x2,y2;
  25.     QGraphicsScene* pq_scn;
  26.     QGraphicsLineItem* pq_line1;       //works
  27.     QGraphicsLineItem* pq_line2;
  28.     QGraphicsLineItemMember* pq_line3; //does not work
  29.     QGraphicsLineItemMember* pq_line4;
  30. };

  1. CL_STATE::CL_STATE( QGraphicsScene* pq_scene ):
  2.     pq_scn(pq_scene)
  3. {
  4.     QPen pen;
  5.     pen.setWidth(2);
  6.  
  7.     //add lines
  8.     pq_line1 = pq_scn->addLine(0,0,0,0,pen);
  9.     pq_line2 = pq_scn->addLine(0,0,0,0,pen);
  10.     pq_line3 = pq_scn->addLine(0,0,0,0,pen);
  11.     pq_line4 = pq_scn->addLine(0,0,0,0,pen);
  12.  
  13.     //make movable
  14.     pq_line1->setFlag(QGraphicsItem::ItemIsMovable);
  15.     pq_line2->setFlag(QGraphicsItem::ItemIsMovable);
  16.     pq_line3->setFlag(QGraphicsItem::ItemIsMovable);
  17.     pq_line4->setFlag(QGraphicsItem::ItemIsMovable);
  18.     pq_line1->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
  19.     pq_line2->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
  20.     pq_line3->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
  21.     pq_line4->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
  22.  
  23.     //set move cursor
  24.     pq_line1->setCursor(Qt::SizeAllCursor);
  25.     pq_line2->setCursor(Qt::SizeAllCursor);
  26.     pq_line3->setCursor(Qt::SizeAllCursor);
  27.     pq_line4->setCursor(Qt::SizeAllCursor);
  28. }

January 28, 2012

cincirin cincirin
Robot Herder
270 posts

If you want to add QGraphicsLineItemMember object to scene you need to use QGraphicsScene::addItem [developer.qt.nokia.com]

  1. QGraphicsLineItemMember* lineItemMember = new QGraphicsLineItemMember();
  2. pq_scn->addItem(lineItemMember);

January 28, 2012

Artemus Artemus
Lab Rat
12 posts

cincirin wrote:
If you want to add QGraphicsLineItemMember object to scene you need to use QGraphicsScene::addItem [developer.qt.nokia.com]
@
QGraphicsLineItemMember* lineItemMember = new QGraphicsLineItemMember();
pq_scn->addItem(lineItemMember);

@


works perfect, thank you!

 
  ‹‹ CSV format!      QMYSQL equivalent of MYSQL source function ››

You must log in to post a reply. Not a member yet? Register here!