Important: Please read the Qt Code of Conduct -

QVector at/operator[] and const

  • [EDIT: split off "this thread": 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:
    void plot(float *data, int size){
    for(int i=0; i<size; i++)[i]);
    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,

  • It's the use of "QVector::at() ": The docs state clearly, that it returns a const reference:

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



    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