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. How to create a multi-dimensional QVector variable in header file with correct size?

How to create a multi-dimensional QVector variable in header file with correct size?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qvectorqvector3d
13 Posts 5 Posters 2.8k Views
  • 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.
  • J JonB
    8 Jan 2021, 15:33

    @CJha
    I don't think you can assign limiting sizes like this in a header file.

    The size is created when you assign values into the object. Which you could do in the .cpp. Why is this a header file issue for you?

    C Offline
    C Offline
    CJha
    wrote on 8 Jan 2021, 15:42 last edited by
    #4

    @JonB Because if I do not do it in header file then I have to go through a process of creating temporary QVectors to assign the right size to my variable. To assign the right size to my variable QVector<QVector<QVector<QVector<QPixmap>>>> myPixVec I have to first create a temporary QVector<QPixmap> of size 3 then assign it twice to a temporary QVector<QVector<QPixmap>> then repeat the process till I have the right size and then assign it to my myPixVec.

    It is a lengthy process and I have to create many multi-dimensional vectors like this in my application.

    J 1 Reply Last reply 8 Jan 2021, 15:46
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 8 Jan 2021, 15:43 last edited by Christian Ehrlicher 1 Aug 2021, 17:28
      #5

      @CJha said in How to create a multi-dimensional QVector variable in header file with correct size?:

      It is a lengthy process and I have to create many multi-dimensional vectors like this in my application.

      No, it's not. Simply use QVector::resize(). It's a simple 43-dim loop

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      C 1 Reply Last reply 8 Jan 2021, 15:44
      0
      • C Christian Ehrlicher
        8 Jan 2021, 15:43

        @CJha said in How to create a multi-dimensional QVector variable in header file with correct size?:

        It is a lengthy process and I have to create many multi-dimensional vectors like this in my application.

        No, it's not. Simply use QVector::resize(). It's a simple 43-dim loop

        C Offline
        C Offline
        CJha
        wrote on 8 Jan 2021, 15:44 last edited by
        #6

        @Christian-Ehrlicher Ok thanks, yes I can use resize. But still, if I can create a one-dimensional vector in my header file with a given size then is there no way to do it for a multi-dimensional vector?

        1 Reply Last reply
        0
        • C CJha
          8 Jan 2021, 15:42

          @JonB Because if I do not do it in header file then I have to go through a process of creating temporary QVectors to assign the right size to my variable. To assign the right size to my variable QVector<QVector<QVector<QVector<QPixmap>>>> myPixVec I have to first create a temporary QVector<QPixmap> of size 3 then assign it twice to a temporary QVector<QVector<QPixmap>> then repeat the process till I have the right size and then assign it to my myPixVec.

          It is a lengthy process and I have to create many multi-dimensional vectors like this in my application.

          J Offline
          J Offline
          JonB
          wrote on 8 Jan 2021, 15:46 last edited by
          #7

          @CJha
          I have absolutely no idea what you are saying you have to do in your most recent, it doesn't sound right at all. That's all I can say.

          Anyway, both @Christian-Ehrlicher & I are saying you don't do this in the declaration in the header, you just do whatever in the .cpp.

          QVectors don't have fixed, compile-time declaration sizes anyway, they are dynamic. Plain C arrays can have fixed sizes.

          C 1 Reply Last reply 8 Jan 2021, 15:58
          1
          • J JonB
            8 Jan 2021, 15:46

            @CJha
            I have absolutely no idea what you are saying you have to do in your most recent, it doesn't sound right at all. That's all I can say.

            Anyway, both @Christian-Ehrlicher & I are saying you don't do this in the declaration in the header, you just do whatever in the .cpp.

            QVectors don't have fixed, compile-time declaration sizes anyway, they are dynamic. Plain C arrays can have fixed sizes.

            C Offline
            C Offline
            CJha
            wrote on 8 Jan 2021, 15:58 last edited by CJha 1 Aug 2021, 16:02
            #8

            @JonB @Christian-Ehrlicher Thanks for your input, I know it is slightly confusing. I will try to elaborate more.
            I need a multi-dimensional vector of size 3, 2, 2, 2. To get this vector I declare a variable QVector<QVector<QVector<QVector<T>>>> my4DVec in my header file. Now if it was a one-dimensional vector, I could simply assign the size while creating it like this

            QVector<T> my1DVec = QVector<T>(5); // Assuming I need the size to be 5
            

            But what I need is a multi-dimensional vector, so why cannot I go like:

            QVector<QVector<QVector<QVector<T>>>> my4DVec = QVector<QVector<QVector<QVector<T>(3)>(2)>(2)>(2);
            

            I know the above syntax is incorrect, what I am trying to ask is that Is there any syntax like this available?

            The other option is to create the vector in header file like this:

            QVector<QVector<QVector<QVector<T>>>> my4DVec;
            

            And then in my .cpp file do this:

            my4DVec.resize(2);
            my4DVec[0].resize(2);
            my4DVec[1].resize(2);
            my4DVec[0][0].resize(2);
            my4DVec[0][1].resize(2);
            my4DVec[1][0].resize(2);
            my4DVec[1][1].resize(2);
            .
            .
            .
            

            You can see that this way of doing it takes longer and is confusing. Also, I know I can use for loop for this, but that will still be lengthy and confusing. So that's why I was asking if there is a way I could do it in header file as I do it with a one-dimensional vector.

            C 1 Reply Last reply 8 Jan 2021, 17:28
            0
            • C CJha
              8 Jan 2021, 15:58

              @JonB @Christian-Ehrlicher Thanks for your input, I know it is slightly confusing. I will try to elaborate more.
              I need a multi-dimensional vector of size 3, 2, 2, 2. To get this vector I declare a variable QVector<QVector<QVector<QVector<T>>>> my4DVec in my header file. Now if it was a one-dimensional vector, I could simply assign the size while creating it like this

              QVector<T> my1DVec = QVector<T>(5); // Assuming I need the size to be 5
              

              But what I need is a multi-dimensional vector, so why cannot I go like:

              QVector<QVector<QVector<QVector<T>>>> my4DVec = QVector<QVector<QVector<QVector<T>(3)>(2)>(2)>(2);
              

              I know the above syntax is incorrect, what I am trying to ask is that Is there any syntax like this available?

              The other option is to create the vector in header file like this:

              QVector<QVector<QVector<QVector<T>>>> my4DVec;
              

              And then in my .cpp file do this:

              my4DVec.resize(2);
              my4DVec[0].resize(2);
              my4DVec[1].resize(2);
              my4DVec[0][0].resize(2);
              my4DVec[0][1].resize(2);
              my4DVec[1][0].resize(2);
              my4DVec[1][1].resize(2);
              .
              .
              .
              

              You can see that this way of doing it takes longer and is confusing. Also, I know I can use for loop for this, but that will still be lengthy and confusing. So that's why I was asking if there is a way I could do it in header file as I do it with a one-dimensional vector.

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 8 Jan 2021, 17:28 last edited by
              #9

              @CJha said in How to create a multi-dimensional QVector variable in header file with correct size?:

              You can see that this way of doing it takes longer and is confusing

              Again: use a loop (or 3 in your case)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • C Online
                C Online
                Chris Kawa
                Lifetime Qt Champion
                wrote on 8 Jan 2021, 18:43 last edited by Chris Kawa 1 Aug 2021, 18:52
                #10

                To do what you want you'd need to have a vector constructor that takes parameters and passes them to the constructors of its elements. There's simply no such thing in QVector, nor in any vector implementation I know.

                The effect you want can be achieved through C++ initializer lists:

                QVector<QVector<QVector<QVector<T>>>> my4DVec {{{{{},{}},{{},{}}},{{{},{}},{{},{}}}},{{{{},{}},{{},{}}},{{{},{}},{{},{}}}}};
                

                It does what you want, but I would argue is horribly confusing to read and wouldn't pass any sane code review ;)
                Just use a loop or resize like others suggested.

                Another option, if you're not planning to resize any of the vectors, is to use an array instead. Then you could simply do:

                std::array<std::array<std::array<std::array<T, 2>, 2>, 2>, 2> my4DVec;
                

                Out of curiosity - what do you need a 4D vector of pixmaps for? Sounds extremely exotic :)

                C 1 Reply Last reply 11 Jan 2021, 08:28
                5
                • C Chris Kawa
                  8 Jan 2021, 18:43

                  To do what you want you'd need to have a vector constructor that takes parameters and passes them to the constructors of its elements. There's simply no such thing in QVector, nor in any vector implementation I know.

                  The effect you want can be achieved through C++ initializer lists:

                  QVector<QVector<QVector<QVector<T>>>> my4DVec {{{{{},{}},{{},{}}},{{{},{}},{{},{}}}},{{{{},{}},{{},{}}},{{{},{}},{{},{}}}}};
                  

                  It does what you want, but I would argue is horribly confusing to read and wouldn't pass any sane code review ;)
                  Just use a loop or resize like others suggested.

                  Another option, if you're not planning to resize any of the vectors, is to use an array instead. Then you could simply do:

                  std::array<std::array<std::array<std::array<T, 2>, 2>, 2>, 2> my4DVec;
                  

                  Out of curiosity - what do you need a 4D vector of pixmaps for? Sounds extremely exotic :)

                  C Offline
                  C Offline
                  CJha
                  wrote on 11 Jan 2021, 08:28 last edited by CJha 1 Nov 2021, 08:30
                  #11

                  @Chris-Kawa Thanks for explaining it to me.

                  Out of curiosity - what do you need a 4D vector of pixmaps for? Sounds extremely exotic :)

                  I need to store different labels which I paint on a QWidget using QPainter. Since the labels are rotated counter-clockwise 90 degrees, I need to use a QPixmap to draw labels otherwise antialiasing doesn't work and texts look weird. I realized that if I make a new label each time update() on my QWidget is called then just the part of making a label takes ~10 ms and if I store all different types of labels in a 4D vector (the labels depends on 4 different factors) of QPixmap then I could just select the correct one from the vector and increase my update() time for the QWidget (this ~10 ms is around 20-25% of my total update() time).

                  J 1 Reply Last reply 11 Jan 2021, 08:38
                  0
                  • C CJha
                    11 Jan 2021, 08:28

                    @Chris-Kawa Thanks for explaining it to me.

                    Out of curiosity - what do you need a 4D vector of pixmaps for? Sounds extremely exotic :)

                    I need to store different labels which I paint on a QWidget using QPainter. Since the labels are rotated counter-clockwise 90 degrees, I need to use a QPixmap to draw labels otherwise antialiasing doesn't work and texts look weird. I realized that if I make a new label each time update() on my QWidget is called then just the part of making a label takes ~10 ms and if I store all different types of labels in a 4D vector (the labels depends on 4 different factors) of QPixmap then I could just select the correct one from the vector and increase my update() time for the QWidget (this ~10 ms is around 20-25% of my total update() time).

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 11 Jan 2021, 08:38 last edited by
                    #12

                    @CJha still, that doesn't mean it has to be a 4D vector, make it 1 dimension and define an access function, that projects the "4d coordinates" to the 1d index.


                    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.

                    C 1 Reply Last reply 11 Jan 2021, 08:46
                    4
                    • J J.Hilk
                      11 Jan 2021, 08:38

                      @CJha still, that doesn't mean it has to be a 4D vector, make it 1 dimension and define an access function, that projects the "4d coordinates" to the 1d index.

                      C Offline
                      C Offline
                      CJha
                      wrote on 11 Jan 2021, 08:46 last edited by
                      #13

                      @J-Hilk Thanks, yes I could do that as well.

                      1 Reply Last reply
                      0

                      13/13

                      11 Jan 2021, 08:46

                      • Login

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