So I am drawing a QLineF based on the angle of a QDial, and I need to adjust the position of the points on the line periodically after it has been drawn in the QGraphicsScene. Basically I need to track the angle of the QDial with the line, but once the line is drawn, I need for the line to begin to move towards the center of the screen. as it stands right now, I just keep drawing the same line over top itself.
Here is how I'm getting the points
void addNewPoint( int qdialAngle ){
double oldX = m_sceneCenter;
double oldY = m_sceneCenter;
double newX = oldX + qCos(qDegreesToRadians(range)) * m_sceneRadius;
double newY = oldY + qCos(qDegreesToRadians(range)) * m_sceneRadius;
m_historyPoint_list.append(pt);
m_historyPoint_count++;
switch(m_historyPoint_count){
case 1:
m_historyPoint_A = pt;
break;
case 2:
m_historyPoint_B = pt;
m_historyPoint_count = 0;
break;
}
}
void addNewPoint( int qdialAngle ){
double oldX = m_sceneCenter;
double oldY = m_sceneCenter;
double newX = oldX + qCos(qDegreesToRadians(range)) * m_sceneRadius;
double newY = oldY + qCos(qDegreesToRadians(range)) * m_sceneRadius;
QPointF pt(newX, newY);
m_historyPoint_list.append(pt);
m_historyPoint_count++;
switch(m_historyPoint_count){
case 1:
m_historyPoint_A = pt;
break;
case 2:
m_historyPoint_B = pt;
m_historyPoint_count = 0;
break;
}
}
To copy to clipboard, switch view to plain text mode
And here is how I'm drawing the line from those points.
void drawPathLine(){
m_HistoryLine
= QLineF(m_historyPoint_A, m_historyPoint_B
);
QPen linePen
(Qt
::green, Qt
::SolidLine);
linePen.setWidth(2);
m_GraphicsScene->addLine(m_HisoryLine, linePen);
}
void drawPathLine(){
m_HistoryLine = QLineF(m_historyPoint_A, m_historyPoint_B);
QPen linePen(Qt::green, Qt::SolidLine);
linePen.setWidth(2);
m_GraphicsScene->addLine(m_HisoryLine, linePen);
}
To copy to clipboard, switch view to plain text mode
This all works fine, I just don't know how to adjust the points on the line afterwards.
If I scale the QGraphicsScene slowly upwards overtime, the effect works for the most part, it worked for the prototype at least, but the problem is that the line gets smaller and smaller as time progresses. QLineF doesn't seem to be able to set the flag ItemIgnoresTransformations which would have worked in this case.
I tried to make pointers of the Points, but QLineF didn't accept pointers.
How can adjust this line once it has been drawn in the scene?
Here's what I'm trying:
void offsetPoints(){ //called from a timer
QLineF adjLine
= line
->line
();
int adjLine_pt1_x = adjLine.p1.x();
int adjLine_pt1_y = adjLine.p1.y();
int adjLine_pt2_x = adjLine.p2.x();
int adjLine_pt2_y = adjLine.p2.y();
QPointF newOffsetP1
= QPointF(getNewPoint
( getBearing
(adjLine_pt1_x, adjLine_pt1_y
),
getRange(adjLine_pt1_x, adjLine_pt1_y));
QPointF newOffsetP2
= QPointF(getNewPoint
( getBearing
(adjLine_pt2_x, adjLine_pt2_y
),
getRange(adjLine_pt2_x, adjLine_pt2_y));
adjLine.setP1(newOffsetP1);
adjLine.setP2(newOffsetP2);
line->setline(adjLine);
}
}
QPointF getNewPoint
(double bearing,
double range
){ double oldX = m_scene_center; //view->size().width()/2
double oldY = m_scene_center;
double newX = oldX + qCos(qDegreesToRadians(bearing)) * (range - m_offsetSpeed); //m_offsetSpeed -= 0.1
double newY = oldY + qSin(qDegreesToRadians(bearing)) * (range - m_offsetSpeed);
return newPoint;
}
double getBearing(int Xpos, int Ypos){
double lat1 = m_center.x();
double lon1 = m_center.y();
double lat2 = Xpos;
double lon2 = Ypos;
double X = qSin(lon2 - lon1) * qCos(lat2);
double Y = qCos(lat1) * qSin(lat2) - qSin(lat1) * qCos(lat2) * qCos(lon2 - lon1);
double bearing = (qAtan2(Y,X) * (180 / 3.14159265), 360);
if(bearing < 0)
bearing += 360;
return bearing;
}
double getRange(int Xpos, int Ypos){
double centerX = m_center.x();
double centerY = m_center.y();
double newX = centerX - Xpos;
double newY = centerY - Ypos;
double distance = qPow(newX, 2) - qPow(newY,2);
double range = qSqrt( distance);
return range;
}
void offsetPoints(){ //called from a timer
foreach(QGraphicsLineItem* line, m_line_list){
QLineF adjLine = line->line();
int adjLine_pt1_x = adjLine.p1.x();
int adjLine_pt1_y = adjLine.p1.y();
int adjLine_pt2_x = adjLine.p2.x();
int adjLine_pt2_y = adjLine.p2.y();
QPointF newOffsetP1 = QPointF(getNewPoint( getBearing(adjLine_pt1_x, adjLine_pt1_y),
getRange(adjLine_pt1_x, adjLine_pt1_y));
QPointF newOffsetP2 = QPointF(getNewPoint( getBearing(adjLine_pt2_x, adjLine_pt2_y),
getRange(adjLine_pt2_x, adjLine_pt2_y));
adjLine.setP1(newOffsetP1);
adjLine.setP2(newOffsetP2);
line->setline(adjLine);
}
}
QPointF getNewPoint(double bearing, double range){
double oldX = m_scene_center; //view->size().width()/2
double oldY = m_scene_center;
double newX = oldX + qCos(qDegreesToRadians(bearing)) * (range - m_offsetSpeed); //m_offsetSpeed -= 0.1
double newY = oldY + qSin(qDegreesToRadians(bearing)) * (range - m_offsetSpeed);
QPointF newPoint = QPointF(newX, newY);
return newPoint;
}
double getBearing(int Xpos, int Ypos){
double lat1 = m_center.x();
double lon1 = m_center.y();
double lat2 = Xpos;
double lon2 = Ypos;
double X = qSin(lon2 - lon1) * qCos(lat2);
double Y = qCos(lat1) * qSin(lat2) - qSin(lat1) * qCos(lat2) * qCos(lon2 - lon1);
double bearing = (qAtan2(Y,X) * (180 / 3.14159265), 360);
if(bearing < 0)
bearing += 360;
return bearing;
}
double getRange(int Xpos, int Ypos){
double centerX = m_center.x();
double centerY = m_center.y();
double newX = centerX - Xpos;
double newY = centerY - Ypos;
double distance = qPow(newX, 2) - qPow(newY,2);
double range = qSqrt( distance);
return range;
}
To copy to clipboard, switch view to plain text mode
The lines definitely are doing something after they are drawn, but their behavior is very erratic. I feel like I'm close, maybe my math is off?
I'm storing the points used to create the lines in my "m_historyPoint_list". My thought was that I should be able to perform these calculations on the points and then the line will "simply" update, but that didn't work because once the points are in the list, they become const points.
Again, if anyone knows of a way that I could apply the ItemIgnoresTransformations to my lines so that as I scale my scene, the lines won't shrnk, most of this headache would go away.
Bookmarks