Results 1 to 11 of 11

Thread: Diagram Scene Example - two overlapping ellipses

  1. #1
    Join Date
    Apr 2017
    Posts
    6
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Diagram Scene Example - two overlapping ellipses

    Hello forum,

    I believe almost everyone in the forum have gone through the diagramscene example that comes along with in the Qt Demo.

    I have included a new shape - ellipse in the editor, using this at DiagramItem:

    Qt Code:
    1. elif self.diagramType == self.Ellipse:
    2. item = QtGui.QGraphicsEllipseItem(-80,-80,160,160)
    3. self.myPolygon = item.shape().toFillPolygon()
    To copy to clipboard, switch view to plain text mode 

    Now, i want to have a new shape with two overlapping ellipses, like the figure below:

    2_circle_venn.gif

    I tried to use Arc, setStartAngle and setSpanAngle, but i was not successful.

    Any hint about it?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Diagram Scene Example - two overlapping ellipses

    Either compose your item from two ellipse items (so that you have a QGraphicsItem with two children) or implement a custom item type (a descendant of QGraphicsShapeItem) where you will draw the custom shape using QPainter.
    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.


  3. The following user says thank you to wysota for this useful post:

    dougluna (20th April 2017)

  4. #3
    Join Date
    Apr 2017
    Posts
    6
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    Hey, thanks for the quick answer!

    I tried using a new QPainterPath, doing this:

    Qt Code:
    1. el1 = QtGui.QGraphicsEllipseItem(-133.3333,-80,160,160).shape()
    2. el2 = QtGui.QGraphicsEllipseItem(-26.66667, -80, 160, 160).shape()
    3. path2 = QtGui.QPainterPath()
    4. path2.addPolygon(el1.toFillPolygon())
    5. path2.addPolygon(el2.toFillPolygon())
    6.  
    7. self.myPolygon = path2.toFillPolygon()
    To copy to clipboard, switch view to plain text mode 

    It's almost perfect, but it also draws a line connecting the two circles.

    Screen Shot 04-18-17 at 09.20 PM.PNG

    I believe that is something related to the moveTo function, but I didn't understand it.

    Any hints?
    Last edited by dougluna; 19th April 2017 at 02:26. Reason: missing [code] tags

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    From the QPainterPath::toFillPolygon() documentation:

    QPolygonF QPainterPath::toFillPolygon(const QTransform &matrix) const

    Converts the path into a polygon using the QTransform matrix, and returns the polygon.

    The polygon is created by first converting all subpaths to polygons, then using a rewinding technique to make sure that overlapping subpaths can be filled using the correct fill rule.

    Note that rewinding inserts addition [sic] lines in the polygon so the outline of the fill polygon does not match the outline of the path.
    Note the last line. Your overlapping ellipses create an indeterminate state, so toFillPolygon() is forced to insert an extra edge to make the winding determinate under the default Qt::OddEvenFill filling rule. If you intend for the ellipses to be filled, you could try setting the fill rule to Qt::WindingFill which might fix the problem. If you aren't going to fill them, then don't call this method.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Apr 2017
    Posts
    6
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    Quote Originally Posted by d_stranz View Post
    From the QPainterPath::toFillPolygon() documentation:



    Note the last line. Your overlapping ellipses create an indeterminate state, so toFillPolygon() is forced to insert an extra edge to make the winding determinate under the default Qt::OddEvenFill filling rule. If you intend for the ellipses to be filled, you could try setting the fill rule to Qt::WindingFill which might fix the problem. If you aren't going to fill them, then don't call this method.
    Hey, thanks!
    I tried to set the Fill Rule as WindingFill, but it didn't fix the problem.

    Qt Code:
    1. path2 = QtGui.QPainterPath()
    2. path2.setFillRule(Qt.WindingFill)
    To copy to clipboard, switch view to plain text mode 

    Any other ideas?

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    I just looked at your original code again and can't understand why you are doing this:

    Qt Code:
    1. el1 = QtGui.QGraphicsEllipseItem(-133.3333,-80,160,160).shape()
    2. el2 = QtGui.QGraphicsEllipseItem(-26.66667, -80, 160, 160).shape()
    3. path2 = QtGui.QPainterPath()
    4. path2.addPolygon(el1.toFillPolygon())
    5. path2.addPolygon(el2.toFillPolygon())
    6.  
    7. self.myPolygon = path2.toFillPolygon()
    To copy to clipboard, switch view to plain text mode 

    Creating two QGraphicsEllipseItem instances just to get their polygons doesn't make a lot of sense, when this would be much more straightforward:

    Qt Code:
    1. path2 = QtGui.QPainterPath()
    2. path2.addEllipse(-133.3333,-80,160,160)
    3. path2.addEllipse(-26.66667, -80, 160, 160)
    4.  
    5. self.myPolygon = path2.toFillPolygon()
    To copy to clipboard, switch view to plain text mode 

    I am also not clear why you need self.myPolygon when there is already a QGraphicsPathItem class. You simply create your QPainterPath, add the two ellipses, and set that path on your QGraphicsPathItem instance. No need for all this conversion to polygons.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #7
    Join Date
    Apr 2017
    Posts
    6
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    Quote Originally Posted by d_stranz View Post
    I am also not clear why you need self.myPolygon when there is already a QGraphicsPathItem class. You simply create your QPainterPath, add the two ellipses, and set that path on your QGraphicsPathItem instance. No need for all this conversion to polygons.
    It's because I'm using the Diagram Scene Example, which DiagramItem class is a child of QtGui.QGraphicsPolygonItem.

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    OK, but I think that it is all those polygon conversions that is causing the extra lines to appear. You can still construct the path using only ellipses and convert the final result to a polygon.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #9
    Join Date
    Apr 2017
    Posts
    6
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    I tried what you've suggested, replacing my code with yours and setting the FillRule as WindingFill, but the line is still there. I don't know what more should I try.

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    I doubt there is anything you can do if you must convert the path to a polygon. It is this conversion that is adding the extra line, as the docs say will happen. In order to create a single, complete polygon from two independent ellipses, it has to connect them.

    You could easily solve this problem if you studied the Diagram Scene example and modified it to accept items derived from QAbstractGraphicsShapeItem instead of limiting it to polygon items only. Then you could add your path item without being forced to convert it to a polygon.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  12. The following user says thank you to d_stranz for this useful post:

    dougluna (28th April 2017)

  13. #11
    Join Date
    Apr 2017
    Posts
    6
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Re: Diagram Scene Example - two overlapping ellipses

    Quote Originally Posted by d_stranz View Post
    I doubt there is anything you can do if you must convert the path to a polygon. It is this conversion that is adding the extra line, as the docs say will happen. In order to create a single, complete polygon from two independent ellipses, it has to connect them.

    You could easily solve this problem if you studied the Diagram Scene example and modified it to accept items derived from QAbstractGraphicsShapeItem instead of limiting it to polygon items only. Then you could add your path item without being forced to convert it to a polygon.
    Yeah, I agree with you. I'll study it. Thanks a lot!

Similar Threads

  1. Re-engineering the Diagram Scene Example - Issues
    By sajis997 in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2012, 11:38
  2. diagram scene example - intersection test
    By sajis997 in forum Newbie
    Replies: 12
    Last Post: 14th September 2011, 18:48
  3. mouse selection of overlapping ellipses
    By imagyne in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2011, 09:59
  4. drawing series of rotated ellipses
    By qtn00b in forum Newbie
    Replies: 1
    Last Post: 17th December 2009, 02:51
  5. Extending the Diagram Scene Example
    By dosto.walla in forum Newbie
    Replies: 1
    Last Post: 7th October 2008, 19:02

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.