Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QVector at/operator[] and const



  • [EDIT: split off "this thread":http://developer.qt.nokia.com/forums/viewthread/5340 of rickbkis as it's a similar, though different problem, Volker]

    Hi guys, I'm not sure if mine is the same problem as rickbkis' but I wanted to try here first before creating a new thread.
    I'm trying to plot a vector of floats with QGLWidget so I have created a vector first to put all my points in there:
    @private: QVector<QVector3D> vertices;@
    And a public function that receives data from an external widget:
    @public:
    void plot(float *data, int size){
    for(int i=0; i<size; i++)
    vertices.at(i).setY(data[i]);
    updateGL();
    }@
    But I'm getting the following error:
    passing 'const QVector3D' as 'this' argument of 'void QVector3D::setY(qreal)' discards qualifiers
    Do you know where exactly is my mistake located and how should I sove it? Thanks for any help,
    Boris



  • It's the use of "QVector::at() ":http://doc.qt.nokia.com/4.7/qvector.html#at. The docs state clearly, that it returns a const reference:

    @
    const T & QVector::at ( int i ) const
    @

    use

    @
    vertices[i]
    @

    in order to get a non-const reference. Watch out, value(i) returns a value, not a reference!


  • Moderators

    The same is true for other containers as well.



  • Thanks Volker, did it and worked... I still have some problems understanding how to code, programming is not my main stuff. Thanks again.



  • I recommend to have a look at some of the C++ tutorials. It helps a lot - not only Qt related.


Log in to reply