PDA

View Full Version : how would you drawPolyLine that has missing segments



vonCZ
6th May 2009, 11:16
I have a QVector, "wellData", of QPoints that I render on a subclassed QWidget like so:


void SkiggleLinePanel::paintEvent(QPaintEvent *e)
{

// QVector<QPoint> wellData;

QPainter painter(this);

painter.drawPolyline(wellData.data(), wellData.size());

}

This works as expected. Suppose, however, that I have 7 QPoints with values like so:

0 1
2 2
1 3
3 -9
7 5
6 6
2 3

These are X and Y values, *except* -9 is actually a NULL data value, and this particular x,y coordinate pair should be thrown out... So instead of one longish line of 7 points, I would want 2 shorter lines of 3 points each. How to do this?

One obvious fix: use QVector's members to find -9, then split it into 2 QVectors, and draw both of these. BUT: the actual problem involves a very long Polyline consisting of thousands of QPoints with many NULL values scattered throughout... so doing this would be very cumbersome. Are there other options here?

wysota
6th May 2009, 15:29
QPainter::drawPolyline() takes an array of points and the number of points to draw so you can obtain what you want using this simple loop:


QVector<QPoint> points;
// fillWithData(points);
//...
int start = 0;
for(int i=0;i<points.size();i++){
QPoint pt = points.at(i);
if(pt.x()==-9 || pt.y()==-9){
// draw from start to the current point
if(start!=i){
painter.drawPolyline(points.constData()+start, i-start);
}
start = i;
}
}
if(start!=points.size()-1){
painter.drawPolyline(points.constData()+start, points.size()-1-start);
}