PDA

View Full Version : How to draw polyline using vector computed from another class



PXMYH
17th November 2013, 22:07
Hi,

I am having trouble draw a polyline from a vector of QPointF which is calculated in another class "getAvgHighTempVec".

Here is my code:



// main.cpp
size_t qpoint_size = c.getAvgHighTempVec().size();
QVector<QPointF> points(qpoint_size);

for(size_t i=0; i < qpoint_size; i++){
points[i].setX(i);
points[i].setY(c.getAvgHighTempVec().at(i));
}

weatherStationGUI.draw(points);



//weatherstationgui.h
namespace Ui {
class weatherstationgui;
}
class weatherstationgui : public QMainWindow
{
Q_OBJECT

public:
explicit weatherstationgui(QWidget *parent = 0);
~weatherstationgui();

void draw( QVector<QPointF> dataIn);

private:
Ui::weatherstationgui *ui;

QVector<QPointF> wellData;

protected:
// paint method
void paintEvent(QPaintEvent *event);
};



//weatherstationgui.cpp
void weatherstationgui::draw(QVector<QPointF> dataIn){
wellData = dataIn;
update();
}

void weatherstationgui::paintEvent(QPaintEvent *event){

// create a painter
QPainter painter(this);

painter.drawPolyline(wellData.data(), static_cast<int>(wellData.size()));
}


In the debugger I can see 'wellData' indeed has the correct QPointF points values, but it just doesn't draw on the main window.

Is there anything wrong with my code?

Thanks

ChrisW67
18th November 2013, 02:28
What are typical values in the points array? As you have presented it these values will be treated as equivalent to pixel coordinates.

PXMYH
18th November 2013, 02:46
yes, you are absolutely right. The values stored in the vector points are the coordinates. It looks like { (1.6, 0) , (2.3, 1) ...} in debugger.

ChrisW67
18th November 2013, 19:48
Your points are being drawn in a few pixels in the top left of the window. You need to scale the values yourself or use a transform on the painter to do it for you.
See Coordinate transforms

PXMYH
18th November 2013, 22:07
Thank you Chris! its been there all the time but I chose to ignore it ;)