Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create a QVector at runtime.

Create a QVector at runtime.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 826 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GunkutA
    wrote on last edited by
    #1

    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?

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • G GunkutA

      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?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #5

      @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);
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • G GunkutA

        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?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #2

        @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".

        1 Reply Last reply
        2
        • G Offline
          G Offline
          GunkutA
          wrote on last edited by
          #3

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

          J.HilkJ 1 Reply Last reply
          0
          • G GunkutA

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

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #4

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

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

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            G 1 Reply Last reply
            1
            • G GunkutA

              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?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #5

              @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);
              

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              3
              • B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #6

                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> >

                1 Reply Last reply
                1
                • J.HilkJ J.Hilk

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

                                 
                  QVector <int>  vectorName;
                  vectorName.resize(receivedHeader.paramAddress);
                  
                  G Offline
                  G Offline
                  GunkutA
                  wrote on last edited by
                  #7

                  @J-Hilk It gives an error :(

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    GunkutA
                    wrote on last edited by
                    #8

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved