Results 1 to 9 of 9

Thread: How to get points drawn by strokePath from outline of QPainterPath

  1. #1
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default How to get points drawn by strokePath from outline of QPainterPath

    Hi.

    Is there any way that we can get points (or polygons) from outline strokePath, generated by QPainter::strokePath function?

    I Was able to get access to points inline stroke (marked as green,yellow lines on attached screen) by doing this:

    Qt Code:
    1. QPolygonF polygon = path.toFillPolygon();
    2. for(int i=0;i < polygon.count(); i++)
    3. {
    4. QLineF lineEdge;
    5. if(i != (polygon.count() - 1))
    6. {
    7. lineEdge.setPoints(polygon.at(i),polygon.at(i+1));
    8. }
    9. else
    10. {
    11. lineEdge.setPoints(polygon.at(i),polygon.at(0));
    12. }
    13.  
    14. if (i%2 == 0)
    15. {
    16. QPen pen(Qt::yellow, 4);
    17. painter->setPen(pen);
    18. }
    19. else
    20. {
    21. QPen pen(Qt::green, 4);
    22. painter->setPen(pen);
    23. }
    24. painter->drawLine(lineEdge);
    25. }
    To copy to clipboard, switch view to plain text mode 

    But how about outline point (marked as black line on attached screen) ? How can we get this?

    qt-quest1.jpg

  2. #2
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to get points drawn by strokePath from outline of QPainterPath

    Solved....

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to get points drawn by strokePath from outline of QPainterPath

    Solved....
    Would be nice if you share your solution for others who may face a similar problem...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to get points drawn by strokePath from outline of QPainterPath

    This is (3 steps - tricky) way, but gives you 100% accuracy of points drawn by QPainter.
    Step 1:
    Get some points, and create SVG file.
    Qt Code:
    1. /// Any QPainter element. e.g QLine/QPolygon etc. fill or not any mix of QPainter shapes.
    2. QPainterPath polygonPath;
    3. polygonPath.moveTo(10.0, 80.0);
    4. polygonPath.lineTo(20.0, 10.0);
    5. polygonPath.lineTo(80.0, 30.0);
    6. polygonPath.lineTo(90.0, 70.0);
    7. polygonPath.closeSubpath();
    8.  
    9. /// To write an SVG file, you first need to configure the output by setting the fileName or outputDevice properties.
    10. QSvgGenerator generator;
    11. string directory = "/home/mydir/";
    12. generator.setFileName(QString::fromStdString(directory + "test.svg"));
    13. /// The resolution is specified in dots per inch, and is used to calculate the physical size of an SVG drawing.
    14. generator.setResolution(90); /// If 90 - coordinates in SVG will be in scale 1:1 for size set in setSize()
    15. generator.setSize(QSize(1024, 768));
    16. generator.setTitle(tr("SVG test"));
    17. generator.setDescription(tr("QPainterPath test"));
    18.  
    19. /// Painting in QSvgGenerator is performed in the same way as for any other paint device.
    20. /// However, it is necessary to use the QPainter::begin() and end() to explicitly begin and end painting on the device.
    21. QPainter painter;
    22. painter.begin(&generator);
    23. painter.drawPath(polygonPath);
    24. painter.end();
    25. /// At this point SVG file was created.
    To copy to clipboard, switch view to plain text mode 

    Step 2

    Use QtXML to open the SVG file as XML and manipulate it using the DOM or stream functions as appropriate.
    In this example the content of test.svg is:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    2. <svg width="288.996mm" height="216.747mm"
    3. xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny">
    4. <title>SVG test</title>
    5. <desc>QPainterPath test</desc>
    6. <defs>
    7. </defs>
    8. <g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" >
    9.  
    10. <path vector-effect="none" fill-rule="evenodd" d="M10,80 L20,10 L80,30 L90,70 L10,80"/>
    11. </g>
    12. </svg>
    To copy to clipboard, switch view to plain text mode 

    Step 3

    You need to parse this:
    Qt Code:
    1. d="M10,80 L20,10 L80,30 L90,70 L10,80"
    To copy to clipboard, switch view to plain text mode 

    Use for example QRegex to remove all unwanted letters M,L,C...etc and pair elements to x,y coordinates.
    Point1(10,80)
    Point2(20,10)
    Point3(80,30)
    Point4(90,70)
    Point5(10,80)
    And you will find what we expected.

  5. The following user says thank you to userman for this useful post:

    d_stranz (6th February 2018)

  6. #5
    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: How to get points drawn by strokePath from outline of QPainterPath

    Thanks for posting your solution. It's clever even if it is a little bit kludgy.

    You might be able to make this simpler by using the QSvgGenerator::setOutputDevice() and a QBuffer to replace the disk file. You could then use that QBuffer in the QDomDocument::setContent() call.
    <=== 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.

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

    userman (7th February 2018)

  8. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get points drawn by strokePath from outline of QPainterPath

    Quote Originally Posted by userman View Post
    This is (3 steps - tricky) way, but gives you 100% accuracy of points drawn by QPainter.
    To be honest I didn't get the point of this all - QPainterPath has a public API for reading all coordinates ?

    Qt Code:
    1. QPainterPath polygonPath;
    2. polygonPath.moveTo(10.0, 80.0);
    3. ...
    4.  
    5. for ( int i = 0; i < polygonPath.elementCount(); i++ )
    6. {
    7. const auto el = polygonPath.elementAt( i );
    8. switch( el.type() )
    9. {
    10. case QPainterPath::MoveToElement:
    11. ....
    12. break;
    13. ....
    14. };
    15. }
    To copy to clipboard, switch view to plain text mode 
    In case you really need a record/replay paint device using QPicture would make more sense as no file IO would be involved. Of course you could also write your own type of paint device for recording QPainter commands like being done f.e in http://qwt.sourceforge.net/class_qwt...nt_device.html.

    Uwe

  9. #7
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to get points drawn by strokePath from outline of QPainterPath

    Quote Originally Posted by Uwe View Post
    To be honest I didn't get the point of this all - QPainterPath has a public API for reading all coordinates ?

    Qt Code:
    1. QPainterPath polygonPath;
    2. polygonPath.moveTo(10.0, 80.0);
    3. ...
    4.  
    5. for ( int i = 0; i < polygonPath.elementCount(); i++ )
    6. {
    7. const auto el = polygonPath.elementAt( i );
    8. switch( el.type() )
    9. {
    10. case QPainterPath::MoveToElement:
    11. ....
    12. break;
    13. ....
    14. };
    15. }
    To copy to clipboard, switch view to plain text mode 
    In case you really need a record/replay paint device using QPicture would make more sense as no file IO would be involved. Of course you could also write your own type of paint device for recording QPainter commands like being done f.e in http://qwt.sourceforge.net/class_qwt...nt_device.html.

    Uwe
    Uwe, you didn't get the point of this all... because you did not read my first post carefully. Please view attached image in first post.
    qt-quest1.jpg
    Yes you can read those points this way
    Qt Code:
    1. polygonPath.elementAt( i );
    To copy to clipboard, switch view to plain text mode 
    But it gives you only the middle points of the QPen, marked as green/yellow dash line on my attached image.
    In my example i need to find points marked as black line (on attached image)
    If you known solution how to do this, please share this info.

    Thanks d_stranz yes I can use QIODevice like this:

    Qt Code:
    1. QBuffer buffer;
    2. buffer.open(QBuffer::ReadWrite);
    3. QSvgGenerator generator;
    4. generator.setOutputDevice(&buffer);
    5. ...
    6. buffer.seek(0);
    7. QString s(buffer.readAll());
    8. qInfo() << s;
    To copy to clipboard, switch view to plain text mode 
    (do not create physical file - but i did not it for illustrate what is going on. For my purpose, I needed those points in XML anyway, but use setOutputDevice for speed up process is a very good idea)

    In example form attached image - has to be created stroke path from red Curve, and read stroke path coordinates from SVG to find those points of black path.
    Or there is better solution Uwe?

  10. #8
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get points drawn by strokePath from outline of QPainterPath

    Quote Originally Posted by userman View Post
    Uwe, you didn't get the point of this all... because you did not read my first post carefully.
    Well, there is QPainterPathStroker that is also internally used to create the QPainterPath that gets finally rendered.
    So what you need to do is to set up the stroker with the attributes of your pen and then the strokedPath can be read like in my previous posting.

    Thought you were talking about the strokedPath in your initial posting as you called it like that.

    Uwe

  11. The following user says thank you to Uwe for this useful post:

    userman (7th February 2018)

  12. #9
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to get points drawn by strokePath from outline of QPainterPath

    So the (other) final solution is:

    Draw along outline points of shape:
    qt-quest1.1.jpg

    Qt Code:
    1. QPainterPath path(QPointF(80, 320));
    2. path.lineTo(QPointF(240,90));
    3. path.lineTo(QPointF(330,240));
    4. path.lineTo(QPointF(620,180));
    5. path.lineTo(QPointF(730,110));
    6.  
    7. stroker.setCapStyle(Qt::RoundCap);
    8. stroker.setJoinStyle(Qt::RoundJoin);
    9. stroker.setWidth(100);
    10.  
    11. QPainterPath strokePath = stroker.createStroke(path).simplified();
    12.  
    13. for (int i = 0; i < strokePath.elementCount()-1; i++)
    14. {
    15. QPainterPath::Element p1 = strokePath.elementAt(i);
    16. QPainterPath::Element p2 = strokePath.elementAt(i+1);
    17. painter->setPen(QPen(i % 2 == 0 ? Qt::red : Qt::green, 1));
    18. painter->drawLine(QPointF(p1.x, p1.y), QPointF(p2.x, p2.y));
    19. }
    To copy to clipboard, switch view to plain text mode 

    Solved.

Similar Threads

  1. Adjusting QLineF points AFTER it has been drawn
    By bauervision in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2017, 13:46
  2. How to draw strokepath
    By rajji_saini in forum Newbie
    Replies: 2
    Last Post: 5th April 2011, 01:11
  3. Remove unused points from a QPainterPath
    By JaV0 in forum Qt Programming
    Replies: 0
    Last Post: 4th March 2010, 08:35
  4. Remove points from a QPainterPath
    By JaV0 in forum Qt Programming
    Replies: 0
    Last Post: 3rd March 2010, 16:27
  5. Checking for intersect of QPainterPath OUTLINE?
    By SonOfGuest in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2009, 00:30

Tags for this Thread

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.