PDA

View Full Version : passing QVector of QPoints to a widget's paintEvent



vonCZ
30th April 2009, 18:29
I have an array of data in the main part of my application. I want to use these values to paint a subclassed widget. I've set it up like so:



// mainApp.cpp

QVector<QPoint> points(4000);

for(int a=0; a<4000; a++)
{
points[a].setX( getX_from_elsewhere() );
points[a].setY( getY_from_elsewhere() );
}

ptrSubclassedWidget->draw(points);





// subClassedWidget.h

private:
QVector<QPoint> wellData;

public:
void draw( QVector<QPoint> dataIn);

protected:
void paintEvent(QPaintEvent *pe);




// subClassedWidget.cpp

void subClassedWidget::draw(QVector<QPoint> dataIn);
{
wellData = dataIn;
update();
}

void subClassedWidget::paintEvent(QPaintEvent *pe)
{
// doesn't work for obvious reason: wellData not an *array* of QPoints, but a Vector of QPoints
painter.drawPolyline(wellData, 4000);
}


So, questions:

1. I know how to draw a polyLine based on an *array* of QPoint. How can I do this using a Vector of QPoints?
2. Should I be passing the QVector to draw() by reference?

zaghaghi
30th April 2009, 18:49
Hi,
you can use data member of QVector to retrieve its data as an array.


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

vonCZ
1st May 2009, 07:35
Thank you! One other thing: I had to replace "4000" with wellData.size():



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