Solved 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?
-
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 aQMap
and map the description to its correspondingQVector
.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 separateQVector
s, then you must create create an array or aQVector< 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?
-
@GunkutA
I may misunderstand the situation, but what's wrong withQVector <int> vectorName; vectorName.resize(receivedHeader.paramAddress);
-
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 aQMap
and map the description to its correspondingQVector
.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, likeQMap<QString, QVector<int> >
orQHash<QString, QVector<int> >
-
@J-Hilk It gives an error :(
-
I think I will go with QMap, thank you all for the clarification and suggestions!