Results 1 to 20 of 21

Thread: erratic behavior of graphics items

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default erratic behavior of graphics items

    Hello forum,

    Please Check the attached image. Screenshot-1.jpg. You can see that two elliptical items are connected by an arrow graphics item.

    If the user clicks the small ellipse inside the large ellipse an arrow item is instantiated. The arrow item is extended with the mouse move and mouse release event.

    I also have over-ridden the following function so that the arrow is adjusted even after the position changes of the elliptical items.

    Qt Code:
    1. QVariant H3DHierarchyArrowDockGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. switch(change)
    4. {
    5. case ItemPositionHasChanged:
    6. foreach(H3DHierarchyArrowGraphicsItem *arrow, m_hierarchyArrowList)
    7. arrow->adjust();
    8. break;
    9. default:
    10. break;
    11. }
    12.  
    13. return QGraphicsItem::itemChange(change,value);
    14. }
    To copy to clipboard, switch view to plain text mode 



    But at some point i get the following output while moving the items inside the scene.
    Bizarre.jpg



    As you can see the arrow is totally detached from the destination item. And the arrow is attached back while moving the elliptical items - not so pleasant behavior, is it?

    To get a functional one i have taken hint from elastic nodes, diagram scene example and voreen editor( Qt based volume rendering editor), but it seems that i have missed some ideas to get it right.

    To provide me with useful hint i am also attaching the corresponding source files that you need to look into.

    H3DHierarchyArrowGraphicsItem.cppH3DHierarchyArrowDockGraphicsItem.cppArrowGraphicsItem.cpp


    If the arrow has both the source and destination item i am doing the intersection test just as in the diagramscene example of Qt so that the destination point always slides on the arc of the ellipse. All the arrows that are created has the small ellipse and another larger ellipse as the destination item.

    In inside the adjust() function i am trying to update the destination point of the arrow which seems not to be working. Please make some time to go through it.


    Thanks
    Sajjad

  2. #2
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: erratic behavior of graphics items

    Hello forum,

    I have not got any reply yet on this issue. Please let me know if my elaboration of the problem is not clear enough.

    If two graphical items of different types are connected to a single arrow at both ends, is mandatory to call the adjust() function of arrow graphics item from the both the graphical items?


    I am looking forward to some feed-back



    Regards
    Sajjad

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: erratic behavior of graphics items

    It would help if you provided a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: erratic behavior of graphics items

    Hi

    I have managed to recreate the same problem i am having in my project by doing the following changes in the diagramscene example:


    Inside the diagramitem.h file i added another item as follows:

    Qt Code:
    1. enum DiagramType
    2. {
    3. Step,
    4. Conditional,
    5. StartEnd,
    6. Io ,
    7. Ellipse
    8. };
    To copy to clipboard, switch view to plain text mode 


    Inside the diagramitem constructor i have added the following:

    Qt Code:
    1. case Ellipse:
    2. {
    3. //declare an ellipse with the bounding rectange
    4. // QGraphicsEllipseItem item(-100,-50,200,100);
    5. QGraphicsEllipseItem item(0,0,200,100);
    6. myPolygon = item.shape().toFillPolygon();
    7. }
    8. break;
    To copy to clipboard, switch view to plain text mode 


    I am sure not how to do this arrow and elliptical intersection test where in my project i have defined the bounding rectangle and shape for the elliptical item as follows:

    Qt Code:
    1. QRectF RootGraphicsItem::boundingRect() const
    2. {
    3. //!get the bounding rectangle of the enclosing text
    4. QRectF rect = m_textGraphicsItem->boundingRect();
    5.  
    6.  
    7. //the following condition decides if the boundary needs
    8. //to be changed
    9. if( rect.width() < m_drawRectMinimumWidth )
    10. rect.setWidth(m_drawRectMinimumWidth);
    11.  
    12. if(rect.height() < m_drawRectMinimumHeight)
    13. rect.setHeight(m_drawRectMinimumHeight);
    14.  
    15. //set the rectangle width with the text item spacing
    16. rect.setWidth(rect.width() + getTextItemSpacing() );
    17.  
    18. //and return the rectangle
    19. return rect;
    20. }
    21.  
    22. //the following function is for better collission detection
    23. QPainterPath RootGraphicsItem::shape() const
    24. {
    25. QRectF ellipse_rect = boundingRect();
    26.  
    27.  
    28. path.addEllipse(ellipse_rect);
    29.  
    30. return path;
    31. }
    To copy to clipboard, switch view to plain text mode 


    There are some affine transformation issues need to be taken care of - i guess and i am not sure how to do it. Do i have call some translation command before calling the ellipse drawing function ? I tried this as well and it became messier.


    Looking forward to some useful hint over this .



    Thanks
    Sajjad

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: erratic behavior of graphics items

    And where do you call prepareGeometryChange()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: erratic behavior of graphics items

    Hi

    i have inserted the function call prepareGeometryChange inside the following function at arrow.cpp of the updated diagram scene example to recreate the problem.


    Qt Code:
    1. void Arrow::updatePosition()
    2. {
    3. prepareGeometryChange();
    4.  
    5. //declare a new line
    6. QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0));
    7. setLine(line);
    8. }
    To copy to clipboard, switch view to plain text mode 


    And the above function is called as follows:


    Qt Code:
    1. QVariant DiagramItem::itemChange(GraphicsItemChange change,
    2. const QVariant &value)
    3. {
    4. //if the dialgram items change is about the item positional change
    5. //then we need to notify the component items about the changes
    6. if (change == QGraphicsItem::ItemPositionChange)
    7. {
    8. //all the arrows connected to
    9. //it need to update its position
    10.  
    11. foreach (Arrow *arrow, arrows)
    12. {
    13. arrow->updatePosition();
    14. }
    15. }
    16.  
    17. return value;
    18. }
    To copy to clipboard, switch view to plain text mode 


    But i am having the same problem - The arrow does not attach to the intersection point. I am attaching all the sources now. I hope that it will be more

    illustrative to solve the issue. If i change the code in diagramitem.cpp as follows the problem is solved here as i mentioned in my previous post.

    Qt Code:
    1. QGraphicsEllipseItem item(-100,-50,200,100);
    2. myPolygon = item.shape().toFillPolygon();
    To copy to clipboard, switch view to plain text mode 

    But in my original project, the graphical items bounding rectangles starting point is from (0,0,width,height). And i do want to implement this intersection test there as well. Please make some time to go through this and help me solve the problem.



    Regards
    Sajjad

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: erratic behavior of graphics items

    I meant prepareGeometryChange() for your RootGraphicsItem class. Its bounding rect depends on the size of the text (and possibly some other things) yet you never inform graphics view the size of the item might have changed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: erratic behavior of graphics items

    In the last post i missed to include the source that i did some changes. Sorry.


    arrow.cppdiagramitem.cppdiagramitem.harrow.h



    Please go through it and suggest me to get it fixed.


    Regards
    Sajjad


    Added after 9 minutes:


    Quote Originally Posted by wysota View Post
    I meant prepareGeometryChange() for your RootGraphicsItem class. Its bounding rect depends on the size of the text (and possibly some other things) yet you never inform graphics view the size of the item might have changed.
    Yes it is true that the size of the bounding rectangle is based on the size of the text and predefined text spacing. And i am defining the size of the item inside the boundingRect() function, do i need to call the prepareGeometryChange() inside the boundingRect() and even if i do i get the following error:

    Qt Code:
    1. RootGraphicsItem.cpp:671: error: passing ‘const RootGraphicsItem’ as ‘this’ argument of ‘void QGraphicsItem::prepareGeometryChange()’ discards qualifiers
    To copy to clipboard, switch view to plain text mode 


    Regards
    Sajjad
    Last edited by sajis997; 8th February 2012 at 11:25.

  9. #9
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: erratic behavior of graphics items

    I call the prepareGeometryhange() inside the definition of boundary rectangle but it gives me the error as follows:

    Qt Code:
    1. RootGraphicsItem.cpp:668: error: passing ‘const RootGraphicsItem’ as ‘this’ argument of ‘void QGraphicsItem::prepareGeometryChange()’ discards qualifiers
    To copy to clipboard, switch view to plain text mode 

    I guess that it is not the right place to call the prepareGeometryChange(), as i am doing it now:

    Qt Code:
    1. QRectF RootGraphicsItem::boundingRect() const
    2. {
    3.  
    4. prepareGeometryChange();
    5.  
    6. //!get the bounding rectangle of the enclosing text
    7. QRectF rect = m_textGraphicsItem->boundingRect();
    8.  
    9. //the following condition decides if the boundary needs
    10. //to be changed
    11. if( rect.width() < m_drawRectMinimumWidth )
    12. rect.setWidth(m_drawRectMinimumWidth);
    13.  
    14. if(rect.height() < m_drawRectMinimumHeight)
    15. rect.setHeight(m_drawRectMinimumHeight);
    16.  
    17. //set the rectangle width with the text item spacing
    18. rect.setWidth(rect.width() + getTextItemSpacing() );
    19.  
    20. //and return the rectangle
    21. return rect;
    22. }
    To copy to clipboard, switch view to plain text mode 


    According to the manual it should be called once the set bounding rectangle is about to change. I am deciding the size before setting the bounding rectangle.

    Any more suggestion ?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: erratic behavior of graphics items

    Calling that method from within boundingRect() wouldn't make sense. You need to call it when the actual change occurs (like when the text or spacing is changed).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: erratic behavior of graphics items

    Hi

    This is what i think as well. I am changing the text later after defining the bounding rectangle.


    Could you please figure out any another reason that the arrow item and elliptical item intersection test is not functioning ?



    Regards
    Sajjad

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: erratic behavior of graphics items

    Quote Originally Posted by sajis997 View Post
    Could you please figure out any another reason that the arrow item and elliptical item intersection test is not functioning ?
    What's wrong with the original reason? Don't you think we should test one thing at a time?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default graphics view coordinate system

    This post has been merged into this thread!

    Hello forum,

    With the help of the forum i have made some changes to the diagram scene example where i added elliptical item as follows:

    Qt Code:
    1. case Ellipse:
    2. {
    3. //declare an ellipse with the bounding rectange
    4. QGraphicsEllipseItem item(-100,-50,200,100);
    5. myPolygon = item.shape().toFillPolygon();
    6. }
    7. break;
    To copy to clipboard, switch view to plain text mode 

    I wanted to use the very same idea to implement the intersection test in another project. In my project i have the graphical item has the following definitions for bounding rectange and shape functions:

    Qt Code:
    1. QRectF RootGraphicsItem::boundingRect() const
    2. {
    3. //!get the bounding rectangle of the enclosing text
    4. QRectF rect = m_textGraphicsItem->boundingRect();
    5.  
    6.  
    7. //the following condition decides if the boundary needs
    8. //to be changed
    9. if( rect.width() < m_drawRectMinimumWidth )
    10. rect.setWidth(m_drawRectMinimumWidth);
    11.  
    12. if(rect.height() < m_drawRectMinimumHeight)
    13. rect.setHeight(m_drawRectMinimumHeight);
    14.  
    15. //set the rectangle width with the text item spacing
    16. rect.setWidth(rect.width() + getTextItemSpacing() );
    17.  
    18. //and return the rectangle
    19. return rect;
    20. }
    21.  
    22. //the following function is for better collission detection
    23. QPainterPath RootGraphicsItem::shape() const
    24. {
    25. QRectF ellipse_rect = boundingRect();
    26.  
    27.  
    28. path.addEllipse(ellipse_rect);
    29.  
    30. return path;
    31. }
    To copy to clipboard, switch view to plain text mode 

    But if i try to add the arrow item to my root graphics item using the line intersection example i get erratic behavior - the arrow is not attached to the item and disappears once you move the item inside the scene. You can also recreate the problem in the diagram scene example if you do the following changes


    Qt Code:
    1. case Ellipse:
    2. {
    3. //declare an ellipse with the bounding rectange
    4. // QGraphicsEllipseItem item(-100,-50,200,100);
    5. QGraphicsEllipseItem item(0,0,200,100);
    6. myPolygon = item.shape().toFillPolygon();
    7. }
    8. break;
    To copy to clipboard, switch view to plain text mode 

    You also need to add the following line in the mainwindow.cpp and some additional changes in diagramitem.h:

    Qt Code:
    1. layout->addWidget(createCellWidget(tr("Ellipse"),DiagramItem::Ellipse),2,0);
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. enum DiagramType
    2. {
    3. Step,
    4. Conditional,
    5. StartEnd,
    6. Io ,
    7. Ellipse
    8. };
    To copy to clipboard, switch view to plain text mode 


    I think i need to adjust some transformational issue here. But i have not found any good reference to it. The manual is not illustrative enough.

    Any reference to this issue will be very helpful



    Regards
    Sajjad
    Last edited by Lykurg; 8th February 2012 at 08:44. Reason: merged

Similar Threads

  1. display graphics items on area
    By quantum.17 in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2010, 11:19
  2. Graphics view with millions of items
    By JovianGhost in forum Qt Programming
    Replies: 13
    Last Post: 26th March 2010, 23:58
  3. Erratic performance of IO operations in Qt4 program
    By jcox23 in forum General Programming
    Replies: 2
    Last Post: 26th January 2010, 17:33
  4. Strange behavior of checkable menu items..
    By jiapei100 in forum Qt Tools
    Replies: 1
    Last Post: 28th October 2009, 19:58
  5. Want custom menu items, like graphics, but how?
    By rm in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2008, 20:52

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.