Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. index out of range problem
Forum Updated to NodeBB v4.3 + New Features

index out of range problem

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 4 Posters 471 Views 3 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.
  • J Offline
    J Offline
    Jimmy01901
    wrote on last edited by
    #1

    I have problem with my c++ program which is connected with qml. I'm trying to add values from QVector to QVariantList(because this type is needed in BarSeries in qml etc.) in for loop. I was adding some cout<< to see where the problem is located, but only what I discovered that my for loop is repeat 8 times with this loop conditions (int i = 0; i<5;i++). My problem is: ASSERT failure in QVector::operator[]: "index out of range" and it is linked to file in which I can see this:{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector::operator[]", "index out of range"); return data()[i]; }

    I also add and to my cpp files. Whole the code is working, only last function doesn't. Here's my code, Transformacja.h:

    #include <QObject>
    #include <fftw3.h>
    #include <iostream>
    #include <QVariant>
    #include <QVariantList>
    using namespace std;
    
    #define Re 0
    #define Im 1
    #define PI 3.14159265359
    
    class Transformacja : public QObject {
        Q_OBJECT
    public:
        Q_INVOKABLE static void wykonaj(QVector<double> sygnal);
    public:
        static int N;
        static QVector<double> A;
        static QVector<double> procenty;
    
        Q_INVOKABLE static QVector<double> get_A();
        Q_INVOKABLE static int get_N();
        Q_INVOKABLE static QVector<double> get_procenty();
        Q_INVOKABLE static QVariantList wektor_lista();
        //const QVector<double>& vector
    };
    

    Transformacja.cpp:

    #include <cmath>
    #include <Transformacja.h>
    #include <QList>
    #include <QVariant>
    #include <QVariantList>
    
    QVector<double> Transformacja::A;
    int Transformacja::N;
    QVector<double> Transformacja::procenty;
    
    
    void Transformacja::wykonaj(QVector<double> sygnal)
    {
        Transformacja::N = sygnal.size(); //okreslenie ilosci probek
    
        //cout<<"ilosc probek: "<<ilosc_probek<<endl;
        //cout<<"Re: "<<Re<<endl;
        //cout<<"Im: "<<Im<<endl;
    
        fftw_complex *T = (fftw_complex*)fftw_malloc(Transformacja::N * sizeof(fftw_complex)); //tablica zespolona PO transformacie
        fftw_complex *U = (fftw_complex*)fftw_malloc(Transformacja::N * sizeof(fftw_complex)); //tablica sygnalu przed transformata
    
    
    
        for(int i=0;i<Transformacja::N;i++)  //zamiana sygnal na fftw_complex U
        {
            U[i][Re] = sygnal[i];
            U[i][Im] = 0;
            //cout<<i+1<<". U zespolone: "<<U[i][Re]<<"+j"<<U[i][Im]<<endl;
        }
    
    
        fftw_plan plan = fftw_plan_dft_1d(Transformacja::N,U,T,FFTW_FORWARD,FFTW_ESTIMATE); //fftw plan czyli deklaracja jak to zrobic, ile razy, z jakiej tablicy, do jakiej tablicy,
        //cout<<"utworzono plan"<<endl;
        fftw_execute(plan);  // wykonanie transformaty czyli w tym miejscu T juz nie powinno byc puste
        //cout<<"wykonano plan"<<endl;
        fftw_destroy_plan(plan); //usuwanie tego planu powyzej bo wsm po co ma byc w pamieci lol
        //cout<<"usunieto plan"<<endl;
        fftw_cleanup(); //jaki cleanup czyli sprzatanie po planie
        //cout<<"wykonano cleanup"<<endl;
    
    
    
           //tablica X ktora ma ilosc probek
        //cout<<"utworzono X"<<endl;
        Transformacja::A.resize(Transformacja::N); //deklaracja ze bedzie ilosc probek
       // cout<<"rozszerzono X"<<endl;
    
    
        for(int i=0;i<Transformacja::N;i++) //przeliczanie transformaty na amplitudy - co to znaczy - wyswietla sie 2*|T|/ilosc probek
        {
            Transformacja::A[i] = 2.0* sqrt( pow(T[i][Re],2)+pow(T[i][Im],2)) / Transformacja::N;
            //cout<<i<<". amplituda: "<<X[i]<<endl;
        }
    
        //cout<<"przepisano X do QVector"<<endl;
        fftw_free(T);
        fftw_free(U);
        //cout<<"zwolniono T i U"<<endl;
        //cout<<"Wykonano pomyslnie transformate :) "<<endl;
    
    
    
    
        //to od razu z bomby robi tablice procentowa
        Transformacja::procenty.resize(Transformacja::N);
        //Transformacja::procenty[3] = 100*Transformacja::A[3]/Transformacja::A[1];
        //cout<<Transformacja::procenty[3]<<endl;
        //cout<<Transformacja::N<<endl;
        for(int i=0;i<Transformacja::N;i++)
        {
            Transformacja::procenty[i] = 100*Transformacja::A[i]/Transformacja::A[1];
    
            /*if(Transformacja::procenty[i]>100||Transformacja::procenty[i]<0)
            {
                cout<<i<<"ta harmoniczna ma wieksza amplitude niz podstawowa"<<endl;
    
            }
            else
            {
    
            }
            cout<<"procentowy udzial "<<i<<"tej harmonicznej:"<<Transformacja::procenty[i]<<endl;
            */
        }
    }
    
    QVector<double> Transformacja::get_A()
    {
        return Transformacja::A;
    }
    int Transformacja::get_N()
    {
        return Transformacja::N;
    }
    QVector<double> Transformacja::get_procenty()
    {
        return Transformacja::procenty;
    }
    
    QVariantList Transformacja::wektor_lista()
    {
        QVector<int> vec = {1,2,3,4,5,6};
        QVariantList lista;
        //lista.append(vec.at(1));
        //cout<<lista.size()<<endl;
        //lista.append(vec.at(2));
        //lista.append(vec.at(3));
        //for(int i=0; i<vector.size()-1; i++){
        //    lista.append(vector[i]);
        //}
        for(int i=0; i<vec.size()-1; i++){ 
            lista.append(vec[i]);
        }
        return lista;
    }
    

    in main.qml I just want to use BarSet to show percentage of harmonics in non-sinusoidal signals:

    BarSeries{
        id: wykres_FFT
        axisX: axisX
        axisY: axisY
        BarSet{
            id: barset
            values: transformacja.wektor_lista()
    //what i want here is values: transformacja.wektor_lista(transformacja.get_procenty()), but for now I'm trying to solve easiest problem....
        }
    }
    

    Could you see where the problem is or could you help me with some tips? Maybe more debugs or sth. Sorry for my English, I'm still learning.

    jsulmJ sierdzioS 2 Replies Last reply
    0
    • J Jimmy01901

      I have problem with my c++ program which is connected with qml. I'm trying to add values from QVector to QVariantList(because this type is needed in BarSeries in qml etc.) in for loop. I was adding some cout<< to see where the problem is located, but only what I discovered that my for loop is repeat 8 times with this loop conditions (int i = 0; i<5;i++). My problem is: ASSERT failure in QVector::operator[]: "index out of range" and it is linked to file in which I can see this:{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector::operator[]", "index out of range"); return data()[i]; }

      I also add and to my cpp files. Whole the code is working, only last function doesn't. Here's my code, Transformacja.h:

      #include <QObject>
      #include <fftw3.h>
      #include <iostream>
      #include <QVariant>
      #include <QVariantList>
      using namespace std;
      
      #define Re 0
      #define Im 1
      #define PI 3.14159265359
      
      class Transformacja : public QObject {
          Q_OBJECT
      public:
          Q_INVOKABLE static void wykonaj(QVector<double> sygnal);
      public:
          static int N;
          static QVector<double> A;
          static QVector<double> procenty;
      
          Q_INVOKABLE static QVector<double> get_A();
          Q_INVOKABLE static int get_N();
          Q_INVOKABLE static QVector<double> get_procenty();
          Q_INVOKABLE static QVariantList wektor_lista();
          //const QVector<double>& vector
      };
      

      Transformacja.cpp:

      #include <cmath>
      #include <Transformacja.h>
      #include <QList>
      #include <QVariant>
      #include <QVariantList>
      
      QVector<double> Transformacja::A;
      int Transformacja::N;
      QVector<double> Transformacja::procenty;
      
      
      void Transformacja::wykonaj(QVector<double> sygnal)
      {
          Transformacja::N = sygnal.size(); //okreslenie ilosci probek
      
          //cout<<"ilosc probek: "<<ilosc_probek<<endl;
          //cout<<"Re: "<<Re<<endl;
          //cout<<"Im: "<<Im<<endl;
      
          fftw_complex *T = (fftw_complex*)fftw_malloc(Transformacja::N * sizeof(fftw_complex)); //tablica zespolona PO transformacie
          fftw_complex *U = (fftw_complex*)fftw_malloc(Transformacja::N * sizeof(fftw_complex)); //tablica sygnalu przed transformata
      
      
      
          for(int i=0;i<Transformacja::N;i++)  //zamiana sygnal na fftw_complex U
          {
              U[i][Re] = sygnal[i];
              U[i][Im] = 0;
              //cout<<i+1<<". U zespolone: "<<U[i][Re]<<"+j"<<U[i][Im]<<endl;
          }
      
      
          fftw_plan plan = fftw_plan_dft_1d(Transformacja::N,U,T,FFTW_FORWARD,FFTW_ESTIMATE); //fftw plan czyli deklaracja jak to zrobic, ile razy, z jakiej tablicy, do jakiej tablicy,
          //cout<<"utworzono plan"<<endl;
          fftw_execute(plan);  // wykonanie transformaty czyli w tym miejscu T juz nie powinno byc puste
          //cout<<"wykonano plan"<<endl;
          fftw_destroy_plan(plan); //usuwanie tego planu powyzej bo wsm po co ma byc w pamieci lol
          //cout<<"usunieto plan"<<endl;
          fftw_cleanup(); //jaki cleanup czyli sprzatanie po planie
          //cout<<"wykonano cleanup"<<endl;
      
      
      
             //tablica X ktora ma ilosc probek
          //cout<<"utworzono X"<<endl;
          Transformacja::A.resize(Transformacja::N); //deklaracja ze bedzie ilosc probek
         // cout<<"rozszerzono X"<<endl;
      
      
          for(int i=0;i<Transformacja::N;i++) //przeliczanie transformaty na amplitudy - co to znaczy - wyswietla sie 2*|T|/ilosc probek
          {
              Transformacja::A[i] = 2.0* sqrt( pow(T[i][Re],2)+pow(T[i][Im],2)) / Transformacja::N;
              //cout<<i<<". amplituda: "<<X[i]<<endl;
          }
      
          //cout<<"przepisano X do QVector"<<endl;
          fftw_free(T);
          fftw_free(U);
          //cout<<"zwolniono T i U"<<endl;
          //cout<<"Wykonano pomyslnie transformate :) "<<endl;
      
      
      
      
          //to od razu z bomby robi tablice procentowa
          Transformacja::procenty.resize(Transformacja::N);
          //Transformacja::procenty[3] = 100*Transformacja::A[3]/Transformacja::A[1];
          //cout<<Transformacja::procenty[3]<<endl;
          //cout<<Transformacja::N<<endl;
          for(int i=0;i<Transformacja::N;i++)
          {
              Transformacja::procenty[i] = 100*Transformacja::A[i]/Transformacja::A[1];
      
              /*if(Transformacja::procenty[i]>100||Transformacja::procenty[i]<0)
              {
                  cout<<i<<"ta harmoniczna ma wieksza amplitude niz podstawowa"<<endl;
      
              }
              else
              {
      
              }
              cout<<"procentowy udzial "<<i<<"tej harmonicznej:"<<Transformacja::procenty[i]<<endl;
              */
          }
      }
      
      QVector<double> Transformacja::get_A()
      {
          return Transformacja::A;
      }
      int Transformacja::get_N()
      {
          return Transformacja::N;
      }
      QVector<double> Transformacja::get_procenty()
      {
          return Transformacja::procenty;
      }
      
      QVariantList Transformacja::wektor_lista()
      {
          QVector<int> vec = {1,2,3,4,5,6};
          QVariantList lista;
          //lista.append(vec.at(1));
          //cout<<lista.size()<<endl;
          //lista.append(vec.at(2));
          //lista.append(vec.at(3));
          //for(int i=0; i<vector.size()-1; i++){
          //    lista.append(vector[i]);
          //}
          for(int i=0; i<vec.size()-1; i++){ 
              lista.append(vec[i]);
          }
          return lista;
      }
      

      in main.qml I just want to use BarSet to show percentage of harmonics in non-sinusoidal signals:

      BarSeries{
          id: wykres_FFT
          axisX: axisX
          axisY: axisY
          BarSet{
              id: barset
              values: transformacja.wektor_lista()
      //what i want here is values: transformacja.wektor_lista(transformacja.get_procenty()), but for now I'm trying to solve easiest problem....
          }
      }
      

      Could you see where the problem is or could you help me with some tips? Maybe more debugs or sth. Sorry for my English, I'm still learning.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Jimmy01901 You posted several loops without saying which one is causing the problem.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • J Jimmy01901

        I have problem with my c++ program which is connected with qml. I'm trying to add values from QVector to QVariantList(because this type is needed in BarSeries in qml etc.) in for loop. I was adding some cout<< to see where the problem is located, but only what I discovered that my for loop is repeat 8 times with this loop conditions (int i = 0; i<5;i++). My problem is: ASSERT failure in QVector::operator[]: "index out of range" and it is linked to file in which I can see this:{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector::operator[]", "index out of range"); return data()[i]; }

        I also add and to my cpp files. Whole the code is working, only last function doesn't. Here's my code, Transformacja.h:

        #include <QObject>
        #include <fftw3.h>
        #include <iostream>
        #include <QVariant>
        #include <QVariantList>
        using namespace std;
        
        #define Re 0
        #define Im 1
        #define PI 3.14159265359
        
        class Transformacja : public QObject {
            Q_OBJECT
        public:
            Q_INVOKABLE static void wykonaj(QVector<double> sygnal);
        public:
            static int N;
            static QVector<double> A;
            static QVector<double> procenty;
        
            Q_INVOKABLE static QVector<double> get_A();
            Q_INVOKABLE static int get_N();
            Q_INVOKABLE static QVector<double> get_procenty();
            Q_INVOKABLE static QVariantList wektor_lista();
            //const QVector<double>& vector
        };
        

        Transformacja.cpp:

        #include <cmath>
        #include <Transformacja.h>
        #include <QList>
        #include <QVariant>
        #include <QVariantList>
        
        QVector<double> Transformacja::A;
        int Transformacja::N;
        QVector<double> Transformacja::procenty;
        
        
        void Transformacja::wykonaj(QVector<double> sygnal)
        {
            Transformacja::N = sygnal.size(); //okreslenie ilosci probek
        
            //cout<<"ilosc probek: "<<ilosc_probek<<endl;
            //cout<<"Re: "<<Re<<endl;
            //cout<<"Im: "<<Im<<endl;
        
            fftw_complex *T = (fftw_complex*)fftw_malloc(Transformacja::N * sizeof(fftw_complex)); //tablica zespolona PO transformacie
            fftw_complex *U = (fftw_complex*)fftw_malloc(Transformacja::N * sizeof(fftw_complex)); //tablica sygnalu przed transformata
        
        
        
            for(int i=0;i<Transformacja::N;i++)  //zamiana sygnal na fftw_complex U
            {
                U[i][Re] = sygnal[i];
                U[i][Im] = 0;
                //cout<<i+1<<". U zespolone: "<<U[i][Re]<<"+j"<<U[i][Im]<<endl;
            }
        
        
            fftw_plan plan = fftw_plan_dft_1d(Transformacja::N,U,T,FFTW_FORWARD,FFTW_ESTIMATE); //fftw plan czyli deklaracja jak to zrobic, ile razy, z jakiej tablicy, do jakiej tablicy,
            //cout<<"utworzono plan"<<endl;
            fftw_execute(plan);  // wykonanie transformaty czyli w tym miejscu T juz nie powinno byc puste
            //cout<<"wykonano plan"<<endl;
            fftw_destroy_plan(plan); //usuwanie tego planu powyzej bo wsm po co ma byc w pamieci lol
            //cout<<"usunieto plan"<<endl;
            fftw_cleanup(); //jaki cleanup czyli sprzatanie po planie
            //cout<<"wykonano cleanup"<<endl;
        
        
        
               //tablica X ktora ma ilosc probek
            //cout<<"utworzono X"<<endl;
            Transformacja::A.resize(Transformacja::N); //deklaracja ze bedzie ilosc probek
           // cout<<"rozszerzono X"<<endl;
        
        
            for(int i=0;i<Transformacja::N;i++) //przeliczanie transformaty na amplitudy - co to znaczy - wyswietla sie 2*|T|/ilosc probek
            {
                Transformacja::A[i] = 2.0* sqrt( pow(T[i][Re],2)+pow(T[i][Im],2)) / Transformacja::N;
                //cout<<i<<". amplituda: "<<X[i]<<endl;
            }
        
            //cout<<"przepisano X do QVector"<<endl;
            fftw_free(T);
            fftw_free(U);
            //cout<<"zwolniono T i U"<<endl;
            //cout<<"Wykonano pomyslnie transformate :) "<<endl;
        
        
        
        
            //to od razu z bomby robi tablice procentowa
            Transformacja::procenty.resize(Transformacja::N);
            //Transformacja::procenty[3] = 100*Transformacja::A[3]/Transformacja::A[1];
            //cout<<Transformacja::procenty[3]<<endl;
            //cout<<Transformacja::N<<endl;
            for(int i=0;i<Transformacja::N;i++)
            {
                Transformacja::procenty[i] = 100*Transformacja::A[i]/Transformacja::A[1];
        
                /*if(Transformacja::procenty[i]>100||Transformacja::procenty[i]<0)
                {
                    cout<<i<<"ta harmoniczna ma wieksza amplitude niz podstawowa"<<endl;
        
                }
                else
                {
        
                }
                cout<<"procentowy udzial "<<i<<"tej harmonicznej:"<<Transformacja::procenty[i]<<endl;
                */
            }
        }
        
        QVector<double> Transformacja::get_A()
        {
            return Transformacja::A;
        }
        int Transformacja::get_N()
        {
            return Transformacja::N;
        }
        QVector<double> Transformacja::get_procenty()
        {
            return Transformacja::procenty;
        }
        
        QVariantList Transformacja::wektor_lista()
        {
            QVector<int> vec = {1,2,3,4,5,6};
            QVariantList lista;
            //lista.append(vec.at(1));
            //cout<<lista.size()<<endl;
            //lista.append(vec.at(2));
            //lista.append(vec.at(3));
            //for(int i=0; i<vector.size()-1; i++){
            //    lista.append(vector[i]);
            //}
            for(int i=0; i<vec.size()-1; i++){ 
                lista.append(vec[i]);
            }
            return lista;
        }
        

        in main.qml I just want to use BarSet to show percentage of harmonics in non-sinusoidal signals:

        BarSeries{
            id: wykres_FFT
            axisX: axisX
            axisY: axisY
            BarSet{
                id: barset
                values: transformacja.wektor_lista()
        //what i want here is values: transformacja.wektor_lista(transformacja.get_procenty()), but for now I'm trying to solve easiest problem....
            }
        }
        

        Could you see where the problem is or could you help me with some tips? Maybe more debugs or sth. Sorry for my English, I'm still learning.

        sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        @Jimmy01901 run with debugger attached and it will show you exactly where the fail is.

        The assert is very clear: you are trying to access an invalid index of the vector (so, index smaller than zero or equal or larger than size()). Where exactly it happens I do not know, you are accessing vector elements in many places in Transformacja::wykonaj(QVector<double> sygnal) without checking size().

        Also, on an unrelated note: try to avoid programming in Polish (or any other language than English). It is very hard to understand mixed-language code. And most programming projects are international so somebody at some time will have a hard time trying to understand your code... like right now when you post it on English-speaking forum ;-) I happen to know Polish but this is just a coincidence.

        (Z(:^

        J 1 Reply Last reply
        1
        • sierdzioS sierdzio

          @Jimmy01901 run with debugger attached and it will show you exactly where the fail is.

          The assert is very clear: you are trying to access an invalid index of the vector (so, index smaller than zero or equal or larger than size()). Where exactly it happens I do not know, you are accessing vector elements in many places in Transformacja::wykonaj(QVector<double> sygnal) without checking size().

          Also, on an unrelated note: try to avoid programming in Polish (or any other language than English). It is very hard to understand mixed-language code. And most programming projects are international so somebody at some time will have a hard time trying to understand your code... like right now when you post it on English-speaking forum ;-) I happen to know Polish but this is just a coincidence.

          J Offline
          J Offline
          Jimmy01901
          wrote on last edited by
          #4

          @sierdzio okey, sory about Polish language, I’m learning programming and it’s easier for me to know „what is where”.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @Jimmy01901 You posted several loops without saying which one is causing the problem.

            J Offline
            J Offline
            Jimmy01901
            wrote on last edited by
            #5

            @jsulm My bad, I’m sorry, the function wektor_lista crashes my app

            jsulmJ Chris KawaC 2 Replies Last reply
            0
            • J Jimmy01901

              @jsulm My bad, I’m sorry, the function wektor_lista crashes my app

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Jimmy01901 said in index out of range problem:

              wektor_lista

              I don't see anything wrong there. Please run in debugger like @sierdzio suggested to see what exactly happens.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J Jimmy01901

                @jsulm My bad, I’m sorry, the function wektor_lista crashes my app

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7

                @Jimmy01901 Your Transformacja::wektor_lista function leaves the last element out (note size()-1), so I'm guessing you're then accessing the list believing there's one more item than it really has.

                Btw. if you just want to make a variant list out of vector elements you can do it like this:

                QVector<int> vec {1,2,3,4,5,6};
                QVariantList list (vec.begin(), vec.end());
                

                Also, since vec is local anyway, why even bother with transforming it. You can just do

                QVariantList Transformacja::wektor_lista()
                {
                   return {1,2,3,4,5,6};
                }
                
                J 1 Reply Last reply
                3
                • Chris KawaC Chris Kawa

                  @Jimmy01901 Your Transformacja::wektor_lista function leaves the last element out (note size()-1), so I'm guessing you're then accessing the list believing there's one more item than it really has.

                  Btw. if you just want to make a variant list out of vector elements you can do it like this:

                  QVector<int> vec {1,2,3,4,5,6};
                  QVariantList list (vec.begin(), vec.end());
                  

                  Also, since vec is local anyway, why even bother with transforming it. You can just do

                  QVariantList Transformacja::wektor_lista()
                  {
                     return {1,2,3,4,5,6};
                  }
                  
                  J Offline
                  J Offline
                  Jimmy01901
                  wrote on last edited by
                  #8

                  @Chris-Kawa I will try this thank you

                  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