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. Problem to reset QVector

Problem to reset QVector

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.6k 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.
  • LodiCodeL Offline
    LodiCodeL Offline
    LodiCode
    wrote on last edited by A Former User
    #1

    Hello guys,

    I made a struct like this:

    struct VeiculoImages{
       int id;
       QByteArray img;
       QDate dateUpload;
    };
    

    I am using a QVector to store it, but i have a problem to delete all, when i use my QVector again its send me error "index out of range"

    i am try use many ways to remove all elements but didn't work.

    
        images.erase(images.begin(), images.end());
    //
    images.clear();
    // 
    for(int i = images.size(); i>=0; i--){
        images.takeAt(i);
    }
    

    thanks alot for you time! sorry for bad english...

    Let's code?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Just use clear(). After that, the vector has a size of zero.

      LodiCodeL 1 Reply Last reply
      2
      • ? A Former User

        Hi! Just use clear(). After that, the vector has a size of zero.

        LodiCodeL Offline
        LodiCodeL Offline
        LodiCode
        wrote on last edited by
        #3

        @Wieland don't work, i still getting out of range when i try to add new itens!

        Let's code?

        ? 1 Reply Last reply
        0
        • LodiCodeL LodiCode

          @Wieland don't work, i still getting out of range when i try to add new itens!

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @LodiCode Please show some code.

          LodiCodeL 1 Reply Last reply
          1
          • ? A Former User

            @LodiCode Please show some code.

            LodiCodeL Offline
            LodiCodeL Offline
            LodiCode
            wrote on last edited by
            #5

            @Wieland

            this is the error
            ASSERT failure in QVector<T>::operator[]: "index out of range", file C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qvector.h, line 433

            my function is

            void Historico::getImages()
            {
                query = Connection::getQueryInstance();
            
            
                ui->edit_imagens->clear();
                images.clear();
            
                query.prepare("SELECT * FROM veiculos_fotos WHERE veiculo_id=:veiculo_id");
                query.bindValue(":veiculo_id", veiculo_id);
                VeiculoImages veic;
                if(query.exec())
                    while(query.next()){
                        veic.id = query.value(0).toInt();
                        veic.img = query.value(1).toByteArray();
                        veic.dateUpload = query.value(3).toDate();
                        images.push_back(veic);
                    }
            
                for(int i =0; i<images.size(); i++){
                    ui->edit_imagens->addItem("ID Foto = "+QString::number(images[i].id)+" | Data Upload ="+ images[i].dateUpload.toString("dd-MM-yyyy"));
                }
                Connection::getInstance(false);
            }
            
            

            my header is

            #ifndef HISTORICO_H
            #define HISTORICO_H
            
            #include <QDialog>
            #include "connection.h"
            
            struct VeiculoImages;
            namespace Ui {
            class Historico;
            }
            
            class Historico : public QDialog
            {
                Q_OBJECT
            
            public:
                explicit Historico(QWidget *parent = 0);
                ~Historico();
            
            private:
                Ui::Historico *ui;
                //private functions;
                void setupHistorico();
                void controlConnections();
            
                //vars
                QSqlQuery query;
                int veiculo_id; // get by placaToID()
                QString proprietarioByID(int);
            
                //moneys
                double gastosTotal = 0;
                double ganhosTotal = 0;
                double lucroTotal  = 0;
                QVector<VeiculoImages> images;
            
            protected slots:
                void generateHistory();
                void placaToID();
                void loadInformations();
            
                //tables load
                void loadGastos();
                void loadGanhos();
                void loadNegociacao();
            
                //image load
                void getImages();
                void loadImages(int);
            };
            
            struct VeiculoImages{
               int id;
               QByteArray img;
               QDate dateUpload;
            };
            #endif // HISTORICO_H
            
            

            Let's code?

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              I don't see any reason why the code shouldn't work. Does the debugger really say that the line with "clear" triggers the exception?

              LodiCodeL 1 Reply Last reply
              1
              • ? A Former User

                I don't see any reason why the code shouldn't work. Does the debugger really say that the line with "clear" triggers the exception?

                LodiCodeL Offline
                LodiCodeL Offline
                LodiCode
                wrote on last edited by
                #7

                @Wieland thanks for you help bro, my problem is solved, the error is logical, the vector and function clear is ok,

                the problem is my connect(), i used a signal CurrentIndexChanged(), now i am change to activated(), when clear() is called it call another function that use the vector to get information about the images, and this function access out of range vector index!

                Let's code?

                1 Reply Last reply
                0
                • Andy314A Offline
                  Andy314A Offline
                  Andy314
                  wrote on last edited by Andy314
                  #8

                  I have not read the whole thread, but in the first code is a simple index error yet.

                  for(int i = images.size(); i>=0; i--){
                      images.takeAt(i);
                  

                  Last index is size()-1 !

                  LodiCodeL 1 Reply Last reply
                  2
                  • Andy314A Andy314

                    I have not read the whole thread, but in the first code is a simple index error yet.

                    for(int i = images.size(); i>=0; i--){
                        images.takeAt(i);
                    

                    Last index is size()-1 !

                    LodiCodeL Offline
                    LodiCodeL Offline
                    LodiCode
                    wrote on last edited by
                    #9

                    @Andy314 you is right my fault!

                    Let's code?

                    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