Results 1 to 14 of 14

Thread: Position of QGraphicsItem on Scene.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Position of QGraphicsItem on Scene.

    Hello,

    im new in progamming wie Qt, so i had try some tutorials.
    One of is:

    http://www.bogotobogo.com/Qt/Qt5_QGr...phicsScene.php

    This is the basic of my code.

    What i want to do:

    On the graphics view i add two ellipse (ellipse1, ellipse2) and a line.

    I change the flags of the ellipses to moveable.

    So now i want that the start point of the line (x1,y2) is on the center of ellipse1 and the end point of the line (x2,y2) is at the center of ellipse2.
    If one ellipse move per mouse, the line should move too.

    If i add the line in this way:
    Qt Code:
    1. line = scene->addLine(ellipse1->scenePos().x(),ellipse1->scenePos().y(),
    2. ellipse2->scenePos().x(),ellipse2->scenePos().y(),outlinePen);
    To copy to clipboard, switch view to plain text mode 

    For the scenPos() function i get 0. But i dont know why.

    Did anyone has an idee ?

    Thx

    dialog.hdialog.cppdialog.hdialog.cpp

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Position of QGraphicsItem on Scene.

    Derive a custom class from QGraphicsEllipseItem.

    Add a setter method that takes a line element and an indicator to which end of the line your item is connected to.

    Then in the itemChange() method of your item class, you move "your" end of the line when ever the item moves.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Position of QGraphicsItem on Scene.

    thx for the answer:

    i had try the modification, on basic of:

    http://www.bogotobogo.com/Qt/Qt5_QGr...phicsItems.php

    A additional class customItem is inherit from QGraphicsItem.

    With this approach i get now the positions on scene from ellipse1 and ellipse2.

    But drawing a line is not possible until now.

    Now there are different options. The line can be member of the Dialog class or is a member of the customItem class.
    For the second option, it should be drawn two lines.
    One from ellipses1 to ellipses2, and one from ellipses2 to ellipses1


    customitem.cppcustomitem.hdialog.cppdialog.h
    Last edited by Andre008; 10th February 2016 at 18:16.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Position of QGraphicsItem on Scene.

    My suggestion was to use a QGraphicsLineItem and adjusting its end points

    Cheers,
    _

  5. #5
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Position of QGraphicsItem on Scene.

    ok, i had tried it with:

    dialog.cpp
    Here i send the line pointer .

    Qt Code:
    1. Dialog::Dialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7.  
    8. scene = new QGraphicsScene(this);
    9. ui->graphicsView->setScene(scene);
    10.  
    11. // create our object and add it to the scene
    12. ellipse1 = new customItem();
    13. ellipse2 = new customItem();
    14. line = new QGraphicsLineItem(100,100,450,300);
    15.  
    16.  
    17. qDebug() << "ellipse1->scenePos().x(): " << ellipse1->scenePos().x();
    18.  
    19. ellipse1->setLine(line,false);
    20. ellipse1->setLine(line,true);
    21.  
    22. scene->addItem(ellipse1);
    23. scene->addItem(ellipse2);
    24. scene->addItem(line);
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Dialog(QWidget *parent = 0);
    7. ~Dialog();
    8.  
    9. private:
    10. Ui::Dialog *ui;
    11.  
    12.  
    13. customItem *ellipse1;
    14. customItem *ellipse2;
    15.  
    16.  
    17. };
    18.  
    19. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 


    customItem.cpp

    Qt Code:
    1. void customItem::setLine(QGraphicsLineItem* newLine, bool side)
    2. {
    3. line = newLine;
    4. startOrEnd = side;
    5. }
    6.  
    7. void customItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    8. {
    9. Pressed = false;
    10. update();
    11. QGraphicsItem::mouseReleaseEvent(event);
    12.  
    13. if(startOrEnd == false)
    14. {
    15. line->line().setLine(this->scenePos().x(),this->scenePos().y(),
    16. line->line().x2(),line->line().y2() );
    17. }
    18.  
    19. if(startOrEnd == true)
    20. {
    21. line->line().setLine(line->line().x1(),line->line().y1(),
    22. this->scenePos().x(),this->scenePos().y());
    23.  
    24. }
    25.  
    26. qDebug()<<"xPos: " << this->scenePos().x();
    27. qDebug()<<"yPos: " << this->scenePos().y();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class customItem : public QGraphicsItem
    2. {
    3. public:
    4. customItem();
    5. .....
    6.  
    7. void setLine(QGraphicsLineItem line, bool side);
    8.  
    9. .....
    10.  
    11. public:
    12.  
    13. bool Pressed;
    14.  
    15. bool startOrEnd;
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 

    But it dont work.
    Last edited by Andre008; 10th February 2016 at 23:04.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Position of QGraphicsItem on Scene.

    First, I thought you wanted the line to be between the two centers, so why are you using x/y?
    Also why use scenePos() instead of just the position?

    The center would be just
    Qt Code:
    1. QPointF center = rect().center();
    To copy to clipboard, switch view to plain text mode 

    It would also be easier to get the updated position after it has changed, instead of manually reacting to mouse events.

    Cheers,
    _

  7. #7
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Position of QGraphicsItem on Scene.

    youre right:

    i had change it to:

    customItem.cpp
    here ist the member variable for the center: mCenter for the ellipse
    Qt Code:
    1. void customItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. QRectF ellipse = boundingRect();
    4.  
    5. mCenter = ellipse.center();
    6.  
    7. qDebug() << "mCenter: " << mCenter;
    8.  
    9. if(Pressed)
    10. {
    11. QPen pen(Qt::red, 3);
    12. painter->setPen(pen);
    13. painter->drawEllipse(ellipse);
    14. }
    15. else
    16. {
    17. QPen pen(Qt::black, 3);
    18. painter->setPen(pen);
    19. painter->drawEllipse(ellipse);
    20. }
    21. }
    22.  
    23.  
    24. void customItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    25. {
    26. Pressed = false;
    27.  
    28.  
    29.  
    30. if(startOrEnd == false)
    31. {
    32. line->line().setP1(mCenter);
    33. }
    34.  
    35.  
    36. if(startOrEnd == true)
    37. {
    38. line->line().setP2(mCenter);
    39. }
    40.  
    41.  
    42. update();
    43. QGraphicsItem::mouseReleaseEvent(event);
    44. }
    To copy to clipboard, switch view to plain text mode 

    but the line do not move

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Position of QGraphicsItem on Scene.

    If look at the method signature of QGraphicsLineItem::line(), which is generally a good idea, you will see that it returns a QLineF object.
    Not a pointer, not a reference, an object.

    You are modifying that object but are not storing or using it in any way.
    So you are modifying a temporary object.

    How would that change anything for the line item?

    Cheers,
    _

  9. #9
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Position of QGraphicsItem on Scene.

    Quote Originally Posted by anda_skoa View Post
    If look at the method signature of QGraphicsLineItem::line(), which is generally a good idea, you will see that it returns a QLineF object.
    Not a pointer, not a reference, an object.

    You are modifying that object but are not storing or using it in any way.
    So you are modifying a temporary object.

    Cheers,
    _
    Ok, now i change the specific parts to QLineF:

    customItem.cpp

    I thought the storing could be made with the function setLine or how can i implement it, to assign a line to an ellipses?.


    Qt Code:
    1. void customItem::setLine(QLineF newLine, bool side)
    2. {
    3. line = newLine;
    4. startOrEnd = side;
    5. }
    6.  
    7. void customItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    8. {
    9. Pressed = false;
    10.  
    11. if(startOrEnd == false)
    12. {
    13. line.setP1(mCenter);
    14. }
    15.  
    16.  
    17. if(startOrEnd == true)
    18. {
    19. line.setP2(mCenter);
    20. }
    21.  
    22. update();
    23. QGraphicsItem::mouseReleaseEvent(event);
    24. }
    To copy to clipboard, switch view to plain text mode 

    customItem.h

    Qt Code:
    1. class customItem : public QGraphicsItem
    2. {
    3. public:
    4. customItem();
    5.  
    6. QRectF boundingRect() const;
    7.  
    8. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    9.  
    10. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    11. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    12.  
    13. void setLine(QLineF newLine, bool side);
    14.  
    15. qreal getXPosEllipse();
    16. qreal getYPosEllipse();
    17. public:
    18.  
    19. bool Pressed;
    20.  
    21. bool startOrEnd;
    22. //QGraphicsLineItem *line;
    23.  
    24. QLineF line;
    25. QPointF mCenter;
    26. };
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. Dialog::Dialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7.  
    8. scene = new QGraphicsScene(this);
    9. ui->graphicsView->setScene(scene);
    10.  
    11. QBrush grayBrush(Qt::gray);
    12. QPen outlinePen(Qt::black);
    13. outlinePen.setWidth(2);
    14.  
    15. // create our object and add it to the scene
    16.  
    17.  
    18. ellipse1 = new customItem();
    19. ellipse2 = new customItem();
    20. line = new QGraphicsLineItem(100,100,450,300);
    21.  
    22. ellipse1->setLine(line->line(),false);
    23. ellipse2->setLine(line->line(),true);
    24.  
    25.  
    26. qDebug() << "ellipse1->getXPosEllipse(): " << ellipse1->getXPosEllipse();
    27. qDebug() << "ellipse1->getYPosEllipse(): " << ellipse1->getYPosEllipse();
    28. scene->addItem(ellipse1);
    29. scene->addItem(ellipse2);
    30. scene->addItem(line);
    31.  
    32.  
    33.  
    34. qDebug() << "ellipse1->getXPosEllipse(): " << ellipse1->getXPosEllipse();
    35. qDebug() << "ellipse1->getYPosEllipse(): " << ellipse1->getYPosEllipse();
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Position of QGraphicsItem on Scene.

    That doesn't even make sense. Now you not even attempting to update the line item anymore.

    The ellipse items need to update the position of the line item, so they need the line item.

    But instead of your previous attempt of changing some random temporary line value, you need to update the line item's value.

    Lets have a look at some C++ basics
    Qt Code:
    1. int a = 5;
    2. int b = a;
    3. b = 3;
    4. // which value does a have?
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. #11
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Position of QGraphicsItem on Scene.

    Quote Originally Posted by anda_skoa View Post
    That doesn't even make sense. Now you not even attempting to update the line item anymore.

    The ellipse items need to update the position of the line item, so they need the line item.

    But instead of your previous attempt of changing some random temporary line value, you need to update the line item's value.

    Lets have a look at some C++ basics
    Qt Code:
    1. int a = 5;
    2. int b = a;
    3. b = 3;
    4. // which value does a have?
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _


    My idee was to make something like this:

    Qt Code:
    1. int a=5;
    2. int *b;
    3.  
    4. b= &a;
    5. *b = 3;
    To copy to clipboard, switch view to plain text mode 

    For this reason i create a local variable line in the class custonItem.

Similar Threads

  1. Scene Mouse Position Slightly Offline :S
    By steadi in forum Newbie
    Replies: 5
    Last Post: 23rd October 2012, 14:43
  2. QGraphics scene position
    By junix in forum Newbie
    Replies: 6
    Last Post: 17th April 2012, 03:41
  3. Cursor Position in scene
    By rogerholmes in forum Newbie
    Replies: 2
    Last Post: 12th March 2010, 15:31
  4. Replies: 0
    Last Post: 16th July 2009, 12:44
  5. Cursor scene position
    By xgoan in forum Qt Programming
    Replies: 1
    Last Post: 26th December 2006, 13:51

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
  •  
Qt is a trademark of The Qt Company.