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

Create a QVector at runtime.



  • Hello, I am trying to create QVectors at run time. Since afterwards I will be using the datas inside of them I need to give them spesific names. (Ex. name of the received address from TCP.)
    I need something like this:

                    QString vectorName =  QString::number(receivedHeader.paramAddress);
                    
                    QVector <int>  vectorName;
    

    But of course it does not let me to do that. Is there an another and easy way to accomplish it?



  • @GunkutA

    Normally there is no need to do that. You, as programmer should know what each vector does and where you use it.

    To label your QVectors anyway, you could put them into a QMap and map the description to its corresponding QVector.

    QVector<int> vec;
    QString vectorName =  QString::number(receivedHeader.paramAddress);
    
    QMap<QVector, QString> vectors;
    
    vectors.insert(vec, vectorName);
    


  • @GunkutA
    If you are saying you need a collection of separate QVectors, then you must create create an array or a QVector< QVector<int> >/QList< QVector<int> > of them, as you would for anything else. C++ does not allow you to create variables with a "run-time name".



  • So I need to use 2 D vector for that. Am I right?


  • Moderators

    @GunkutA
    I may misunderstand the situation, but what's wrong with

                   
    QVector <int>  vectorName;
    vectorName.resize(receivedHeader.paramAddress);
    


  • @GunkutA

    Normally there is no need to do that. You, as programmer should know what each vector does and where you use it.

    To label your QVectors anyway, you could put them into a QMap and map the description to its corresponding QVector.

    QVector<int> vec;
    QString vectorName =  QString::number(receivedHeader.paramAddress);
    
    QMap<QVector, QString> vectors;
    
    vectors.insert(vec, vectorName);
    


  • A variable's name is not related to any string in C++.
    You can use a QMap / QHash, like QMap<QString, QVector<int> > or QHash<QString, QVector<int> >



  • @J-Hilk It gives an error :(



  • I think I will go with QMap, thank you all for the clarification and suggestions!


Log in to reply