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 use a QVector with a custom class

How to use a QVector with a custom class

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.1k 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.
  • kasysbiK Offline
    kasysbiK Offline
    kasysbi
    wrote on last edited by
    #1

    Hello everybody...i'm triying to use a QVector with a custom class, can please someone explain me that mecanism? i try this QVector<CoupleStride>.... but the compilation return an error looking like this "ASSERT failure in QVector<T>::operator[]: "index out of range"...

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      There is nothing special QVector needs
      and can be used anywhere in code.

      What it says is that you use a index that is too big.

      So you have error in your code.
      Some where you do
      mylist[INDEX] and index is not
      with in legal range.

      -compilation return an error

      It only happens when you RUN the app, right ?

      also:
      if you only do
      QVector<CoupleStride> mylist;

      The list is empty!
      you must append some CoupleStride to it.

      1 Reply Last reply
      1
      • kasysbiK Offline
        kasysbiK Offline
        kasysbi
        wrote on last edited by
        #3

        It only happens when you RUN the app, right ?// yes
        In fact, i'm triying to return that QVector<CoupleStride> in a function.

        mrjjM 1 Reply Last reply
        0
        • kasysbiK kasysbi

          It only happens when you RUN the app, right ?// yes
          In fact, i'm triying to return that QVector<CoupleStride> in a function.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @kasysbi

          well the error is from crashing the list using [].
          Returning it should not crash it.

          can you show the crashing code ?
          You can single step with debugger to find it or
          look in the call stack after the crash.

          1 Reply Last reply
          0
          • kasysbiK Offline
            kasysbiK Offline
            kasysbi
            wrote on last edited by
            #5

            this appear on the screen
            ASSERT failure in QVector<T>::operator[]: "index out of range", file..........
            ......\Qt\Qt5.2.1\5.2.1\mingw48_32\include/QtCore/qvector.h.line 369
            ...
            end with the code 3

            Taz742T 2 Replies Last reply
            0
            • kasysbiK kasysbi

              this appear on the screen
              ASSERT failure in QVector<T>::operator[]: "index out of range", file..........
              ......\Qt\Qt5.2.1\5.2.1\mingw48_32\include/QtCore/qvector.h.line 369
              ...
              end with the code 3

              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by Taz742
              #6

              @kasysbi hi.
              can you post your full code?

              Do what you want.

              1 Reply Last reply
              1
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                as @Taz742 says, we need to see your code
                where you use the list.
                Also where you append to list and what size you except it to be.

                1 Reply Last reply
                0
                • kasysbiK kasysbi

                  this appear on the screen
                  ASSERT failure in QVector<T>::operator[]: "index out of range", file..........
                  ......\Qt\Qt5.2.1\5.2.1\mingw48_32\include/QtCore/qvector.h.line 369
                  ...
                  end with the code 3

                  Taz742T Offline
                  Taz742T Offline
                  Taz742
                  wrote on last edited by Taz742
                  #8

                  @kasysbi
                  Use this:

                  #include "mainwindow.h"
                  #include <QApplication>
                  #include "QLocale"
                  #include "QDebug"
                  #include "QJsonArray"
                  #include "QJsonDocument"
                  #include "QJsonValue"
                  #include "QJsonObject"
                  #include "QTextCodec"
                  
                  class QtIsMyLife{
                      public:
                          int Age;
                          QString Name;
                          QString Familyname;
                  
                          QtIsMyLife(int _age, QString _name, QString _familyname) {
                              this->Age = _age;
                              this->Name = _name;
                              this->Familyname = _familyname;
                          }
                  };
                  
                  QVector<QtIsMyLife*> GetNewVector() {
                      QVector<QtIsMyLife*> my_vector;
                      my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                  
                      return my_vector;
                  }
                  
                  void GetNewVector(QVector<QtIsMyLife*> &vec) {
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                      vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                  }
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      QVector<QtIsMyLife*> vec = GetNewVector();
                  
                      qDebug() << vec.size();
                  
                      QVector<QtIsMyLife*> vec_2;
                  
                      GetNewVector(vec_2);
                  
                      qDebug() << vec_2.size();
                  
                      return a.exec();
                  }
                  

                  Do what you want.

                  kasysbiK 1 Reply Last reply
                  1
                  • Taz742T Taz742

                    @kasysbi
                    Use this:

                    #include "mainwindow.h"
                    #include <QApplication>
                    #include "QLocale"
                    #include "QDebug"
                    #include "QJsonArray"
                    #include "QJsonDocument"
                    #include "QJsonValue"
                    #include "QJsonObject"
                    #include "QTextCodec"
                    
                    class QtIsMyLife{
                        public:
                            int Age;
                            QString Name;
                            QString Familyname;
                    
                            QtIsMyLife(int _age, QString _name, QString _familyname) {
                                this->Age = _age;
                                this->Name = _name;
                                this->Familyname = _familyname;
                            }
                    };
                    
                    QVector<QtIsMyLife*> GetNewVector() {
                        QVector<QtIsMyLife*> my_vector;
                        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                    
                        return my_vector;
                    }
                    
                    void GetNewVector(QVector<QtIsMyLife*> &vec) {
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
                    }
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        MainWindow w;
                        w.show();
                    
                        QVector<QtIsMyLife*> vec = GetNewVector();
                    
                        qDebug() << vec.size();
                    
                        QVector<QtIsMyLife*> vec_2;
                    
                        GetNewVector(vec_2);
                    
                        qDebug() << vec_2.size();
                    
                        return a.exec();
                    }
                    
                    kasysbiK Offline
                    kasysbiK Offline
                    kasysbi
                    wrote on last edited by
                    #9

                    @Taz742
                    that was very helpfull thanks a lot...and it seems like use "data" pointer is better than use the [] operator with the main vector...thanks!

                    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