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 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  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

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  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

    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.

  7. #7
    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 ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  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

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  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

    I am sorry. I did not get your last post. Did i cause something to distract from the main issue here. Let me summarize again:

    I am trying to make connections between arrow and elliptical items in my own project and i tried to use the concept of diagramscene example.

    I am having trouble to implement it - as the arrow is not properly attached to the elliptical items and they suddenly disappear once i start to move the elliptical items in the scene view.

    I recreated the problem in the diagram scene example and posted in the forum for your point of view. All the details are mentioned in the previous posts.

    Now please guide me where should i start from to debug this problem ?


    Regards
    Sajjad

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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
    Hi

    I am sorry. I did not get your last post. Did i cause something to distract from the main issue here. Let me summarize again:

    I am trying to make connections between arrow and elliptical items in my own project and i tried to use the concept of diagramscene example.

    I am having trouble to implement it - as the arrow is not properly attached to the elliptical items and they suddenly disappear once i start to move the elliptical items in the scene view.

    I recreated the problem in the diagram scene example and posted in the forum for your point of view. All the details are mentioned in the previous posts.

    Now please guide me where should i start from to debug this problem ?
    Correct the bounding rect issue as it may have influence on how your scene is rendered in the view. Once that is done and it's not something that causes the misbehaviour, we can look elsewhere. Otherwise you can prepare a minimal compilable example reproducing the problem and we can work on that.
    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 Re: erratic behavior of graphics items

    Hi

    I would like to know what do i have to do to correct the bounding rect issue of the elliptical graphics item. Its bounding rectangle is based on the bounding rectangle of the text graphics item it contains. Text graphics item that is contained inside the elliptical item is in turn the subclass of QTextGraphicsItem. I would like to repeat that once that the bounding rect is defined inside the boundingRect() it is never changed since then and this is why i do not see any reason to call the prepareGeometryChange() function.

    Could you please mention what might be wrong in the bounding rect function

    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 

    Otherwise i am attaching the files again which is compilable that recreates the problem in another project. All that you need to do is that create another copy of the diagramscene example, remove the diagramscene.h and diagramscene.cpp and add the following files:

    diagramsceneview.h
    diagramsceneview.cpp
    diagramitem.h
    diagramitem.cpp

    And you have add the following line inside the createToolBox() of mainwindow.cpp

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

    If you mange some time and compile with these added files and the reamaining files as arrived with the example you will see the problem

    The arrow is not connected as it is supposed to .


    Let me know if you have any trouble to compile them .


    Regards
    Sajjad
    Attached Files Attached Files

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