[solved] creating a const QVector with values



  • I may seem a dumb, but I haven't figured out yet how to create a const QVector with some values initialized. With a standard vector I would just do this:
    @const int myarray[5] = {1, 2, 3, 4, 5};@
    But I'm unable to do the same with a QVector. If I try to initialize it with some values like I did previously:
    @const QVector myarray(5) = {1, 2, 3, 4, 5};@
    ...the compiler complains that there's a syntax error ( syntax error: missing ';' before '=' ) besides other errors. If instead I first declare it and then try to assign some values...
    @const QVector myarray(5);
    myarray[0]=1;
    myarray[1]=2;
    myarray[2]=3;
    myarray[3]=4;
    myarray[4]=5;@
    ...the compiler complains that myarray is declared as const so you cannot assign values to a const item (obviously).

    So, how do I create and initialize a const QVector? The documentation didn't help at all so please don't link it if there's not a good reason for doing so.



  • If you have C++11 extensions turned on then these work using std::initializer_list:
    @
    const QVector<int> vector( { 1, 2, 3, 4, 5 } );
    const QVector<int> vector = { 1, 2, 3, 4, 5 };
    @
    If not then these are options:
    @
    const QVector<int> vector( QVector<int>() << 1 << 2 << 3 << 4 << 5 );
    const QVector<int> vector = QVector<int>() << 1 << 2 << 3 << 4 << 5;
    @



  • Thank you for your help.
    I tried compiling with C++11 by adding CONFIG + = C++11 to the project .pro file. Unfortunately the first two ways didn't work. They would return the error:
    @C:\Users\T3STY\Documents\Qt\Projects\qcp_test1\qcp_test1\main.cpp:116: error: C2552: 'vector' : non-aggregates cannot be initialized with initializer list
    'QVector<T>' : Types with private or protected data members are not aggregate
    with
    [
    T=int
    ]@

    I tried many ways but still nothing worked. So maybe I should explain you what I'm trying to achieve and find another solution.

    I'm working with QCustomPlot for plotting a graph and I'm assigning a QVector<double> and a QVector<QString> to the xAxis and yAxis to show some custom axis labels and grid. Right now I'm using the next code:
    @/// xAxis scale and labels
    QVector<double> xScale(16); QVector<QString> xScaleLabels(16);
    xScale[0] = 20; xScaleLabels[0] = "20";
    xScale[1] = 40; xScaleLabels[1] = "40";
    xScale[2] = 60; xScaleLabels[2] = "60";
    xScale[3] = 80; xScaleLabels[3] = "80";
    xScale[4] = 100; xScaleLabels[4] = "100";
    xScale[5] = 200; xScaleLabels[5] = "200";
    xScale[6] = 400; xScaleLabels[6] = "400";
    xScale[7] = 600; xScaleLabels[7] = "600";
    xScale[8] = 800; xScaleLabels[8] = "800";
    xScale[9] = 1000; xScaleLabels[9] = "1K";
    xScale[10] = 2000; xScaleLabels[10] = "2K";
    xScale[11] = 4000; xScaleLabels[11] = "4K";
    xScale[12] = 6000; xScaleLabels[12] = "6K";
    xScale[13] = 8000; xScaleLabels[13] = "8K";
    xScale[14] = 10000; xScaleLabels[14] = "10K";
    xScale[15] = 20000; xScaleLabels[15] = "20K";

    /// yAxis scale and labels
    QVector<double> yScale(11); QVector<QString> yScaleLabels(11);
    yScale[0] = -10; yScaleLabels[0] = "-10 db";
    yScale[1] = -8; yScaleLabels[1] = "-8 db";
    yScale[2] = -6; yScaleLabels[2] = "-6 db";
    yScale[3] = -4; yScaleLabels[3] = "-4 db";
    yScale[4] = -2; yScaleLabels[4] = "-2 db";
    yScale[5] = 0; yScaleLabels[5] = " 0 db";
    yScale[6] = +2; yScaleLabels[6] = "+2 db";
    yScale[7] = +4; yScaleLabels[7] = "+4 db";
    yScale[8] = +6; yScaleLabels[8] = "+6 db";
    yScale[9] = +8; yScaleLabels[9] = "+8 db";
    yScale[10] = +10; yScaleLabels[10] ="+10 db";

    /// Assiging the values to the xAxis and yAxis
    graph->xAxis->setTickVector(xScale);
    graph->xAxis->setTickVectorLabels(xScaleLabels);
    graph->yAxis->setTickVector(yScale);
    graph->yAxis->setTickVectorLabels(yScaleLabels);@
    Although it works fine, the QVector s are not declared as const so they are allocated in RAM for being able to change the values. But I don't need to change them at runtime, they should remain the same during the whole application execution. Moreover, I will only call the functions for setting the values and labels once at program launch, so there is no further need of these QVector s to be stored in RAM.
    As long as I'm unable to make a const QVector, is there any other way I can store the QVector s in program memory instead of RAM?


  • Moderators

    Hi,

    [quote author="T3STY" date="1380064394"]As long as I'm unable to make a const QVector, is there any other way I can store the QVector s in program memory instead of RAM?[/quote]Internally, QVector stores all its data on the heap, so there's no way to embed a QVector into static memory.

    Technically, program memory has to be loaded into RAM too before your program can run. (I think you meant "data memory" instead of "RAM"?)

    But anyway, you have 27 doubles and 27 QStrings. It takes less than 1 KB to hold them all, so I wouldn't worry about where they are stored.

    [quote author="T3STY" date="1380064394"]I will only call the functions for setting the values and labels once at program launch, so there is no further need of these QVector s to be stored in RAM.[/quote]Then create the QVectors as local variables, and they will be destroyed right after you set your scales.

    [quote author="T3STY" date="1380064394"]error: C2552: 'vector' : non-aggregates cannot be initialized with initializer list[/quote]What version is your compiler? That looks like a Clang error message. Initializer lists are only supported on Clang 3.1 and newer: http://clang.llvm.org/cxx_status.html



  • bq. I tried compiling with C++11 by adding CONFIG + = C++11 to the project .pro file. Unfortunately the first two ways didn’t work.

    @
    #include <QtCore>
    const QVector<int> vector({1, 2, 3, 4, 5});
    // OR static const QVector<int> vector({1, 2, 3, 4, 5});

    int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    qDebug() << vector.size() << vector;
    return 0;
    }
    @
    @
    TEMPLATE = app
    TARGET = test
    INCLUDEPATH += .
    CONFIG += c++11
    SOURCES += main.cpp
    @
    Works fine here with the Qt5.1.0 binaries on Linux and gcc 4.6.3.

    The "c++11" CONFIG option does not exist in Qt 4 although I was able to build the cpp file manually by adding "-std=c++0x" as the error message suggested.

    In any case, the other options work without that.



  • Yes, I meant data memory, my mistake there.

    I'm running Qt 5.1.1 with MSVC 2012 Compiler (both x86 and x64 versions are available). Maybe that's why I get the error before and ChrisW67's code is not working for me?

    Anyway, I guess I won't bother too much about it since QVectors store values on the heap anyway.
    Thank you for your help guys!


  • Moderators

    You're welcome :)

    Whoops, that error message was from MSVC, not Clang. Anyway, it looks like MSVC 2012 doesn't support initialization lists properly: http://stackoverflow.com/questions/12654394/initializer-list-not-working-with-vector-in-visual-studio-2012



  • Yep... After I posted I made a research about VS and C++11 capabilities and found out this:
    http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
    Looks like at least half the C++11 specifications are unsupported right now, including the initializer lists (which is what the compiler was complaining in the error above). I'll install the MinGW compiler and try again just for fun, but I suppose it will work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.