July 21, 2011

taraj taraj
Lab Rat
1 posts

Qt3 to Qt4, drawing maps with QPainterPath, Qt joining QPainterPath’s together?

 

Hi

I am converting an application from Qt3 to Qt4 and i am having trouble with drawing. Use to use QPointArray’s to do the drawing, but now have to use QPainterPath.

I have a 17 sided closed shape (which looks a bit like a hexagon) which display using QPainterPath on a QPainter on a QWidget. When the shape is drawn in the centre of the screen it displays ok. When I pan the shape up, down, left and right the shape moves out of the window coordinates and it is clipped and correctly displayed.
When i move the shape so that it is half out of the screen on the left and half off the screen to the bottom it draws a line from the last clipped point at the bottom of the screen to the first clipped point that is now visible on the left hand side of the screen, i don’t want it to do this!!

In the first block of code (not the struct) it recognises that the closed polgon is broken when panned to bottom left corner and that point p2 has been clipped and a new Arc is created so ring->ArcList.size() is now 2. But somewhere it is joining all the ring->ArcLists together to become closed, and i don’t know where, could drawpath be doing it?
I have tried using moveTo() and closeSubPath() but that doesn’t fix it, unless i am doing it in the wrong place?

I would like to attached a jpeg picture but can’t.

  1. struct MapRing
  2. {
  3.   Q_GeoPoint centre;
  4.   double internal_radius;
  5.   double external_radius;
  6.   double start_angle;
  7.   double end_angle;
  8.   int surface_pattern;
  9.   int thickness;
  10.   int dash;
  11.   int expand;
  12.   int border_display;
  13.   int surface_display;
  14.   QPainterPath ExternalPoints;
  15.   QPainterPathInternalPoints;
  16.   QList<QPainterPath*> ArcList;
  17.   QPainterPath SurfPoints;  // Invisible outline for fill
  18. };

  1.  MapRing ring;
  2.  int ArcIndex;
  3.         bool first;
  4.         bool p1Clipped;
  5.         bool p2Clipped;
  6.        
  7.         QPoint start;
  8.         QPoint end;
  9.         QPoint p1;
  10.         QPoint p2;
  11.  
  12.         QPainterPath *Arc;
  13.  
  14.         QPolygon polygonF;
  15.  
  16.  
  17.         ring->ArcList.clear();
  18.        
  19.         first = true;      
  20.  
  21.         start.setX(internal_points.elementAt(1).x);  
  22.         start.setY(internal_points.elementAt(1).y);  
  23.        
  24.         polygonF.clear();
  25.  
  26.  
  27.         for(uint i = 2; i < internal_points.elementCount(); i++)
  28.         {
  29.           end.setX(internal_points.elementAt(i).x);
  30.           end.setY(internal_points.elementAt(i).y);
  31.  
  32.           p1 = start;
  33.           p2 = end;
  34.  
  35.           if(clip_rect.clipLine(p1, p2, p1Clipped, p2Clipped))
  36.           {
  37.  
  38.             if(first == true)
  39.             {
  40.               // Start a new line  
  41.               Arc = new QPainterPath;
  42.               ArcIndex = 0;
  43.               ring->ArcList.append(Arc);
  44.               polygonF << QPoint(p1.x(), p1.y());
  45.  
  46.               Arc->addPolygon(polygonF);
  47.  
  48.               ArcIndex++;
  49.               first = false;
  50.             }
  51.    
  52.             polygonF << QPoint(p2.x(), p2.y());
  53.             Arc->addPolygon(polygonF);
  54.  
  55.             ArcIndex++;
  56.  
  57.             if(p2Clipped == true)
  58.             {
  59.               first = true;
  60.             }
  61.           }
  62.           start = end;
  63.          }
  64.          

To draw the map rings to the window i do the following
  1.         MapRing *ring;
  2.  
  3.     for(int i = 0; i < RingList.size(); ++i)
  4.     {
  5.       ring = RingList.at(i);
  6.       if(ring->border_display)
  7.       {      
  8.         SetPenStyle(ring->dash, ring->thickness, &pen;);
  9.         window->paint.setPen(pen);
  10.  
  11.         if(ring->InternalPoints.elementCount() > 2)
  12.         {
  13.           window->paint.drawPath(ring->InternalPoints);
  14.         }
  15.  
  16.         if(ring->ExternalPoints.elementCount() > 2)
  17.         {
  18.           window->paint.drawPath(ring->ExternalPoints); //drawPolygon
  19.         }
  20.  
  21.         qDebug("ring->ArcList.size() %d", ring->ArcList.size());
  22.         for(int i = 0; i < ring->ArcList.size(); ++i)
  23.         {
  24.           if(ring->ArcList.at(i)->elementCount() > 0)
  25.           {
  26.             window->paint.drawPath(*ring->ArcList.at(i));//was drawPolyline(*arc); in qt3
  27.           }
  28.         }
  29.       }
  30.   }

I have been working on this for days now. I was not the original writer of the Qt3 code so am not expert in drawing.

Thank you very much!

0 replies

 
  ‹‹ How to create array of objects at runtime.      [solved] Returnvalue QDialog ››

You must log in to post a reply. Not a member yet? Register here!