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
Forum Update on Monday, May 27th 2025

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.
  • L Offline
    L Offline
    LodiCode
    wrote on 12 Mar 2017, 15:41 last edited by A Former User 3 Dec 2017, 16:07
    #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 12 Mar 2017, 16:07 last edited by
      #2

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

      L 1 Reply Last reply 12 Mar 2017, 16:10
      2
      • ? A Former User
        12 Mar 2017, 16:07

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

        L Offline
        L Offline
        LodiCode
        wrote on 12 Mar 2017, 16:10 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 12 Mar 2017, 16:12
        0
        • L LodiCode
          12 Mar 2017, 16:10

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

          ? Offline
          ? Offline
          A Former User
          wrote on 12 Mar 2017, 16:12 last edited by
          #4

          @LodiCode Please show some code.

          L 1 Reply Last reply 12 Mar 2017, 16:14
          1
          • ? A Former User
            12 Mar 2017, 16:12

            @LodiCode Please show some code.

            L Offline
            L Offline
            LodiCode
            wrote on 12 Mar 2017, 16:14 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 12 Mar 2017, 16:39 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?

              L 1 Reply Last reply 12 Mar 2017, 16:54
              1
              • ? A Former User
                12 Mar 2017, 16:39

                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?

                L Offline
                L Offline
                LodiCode
                wrote on 12 Mar 2017, 16:54 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 12 Mar 2017, 19:07 last edited by Andy314 3 Dec 2017, 19:08
                  #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 !

                  L 1 Reply Last reply 12 Mar 2017, 19:09
                  2
                  • Andy314A Andy314
                    12 Mar 2017, 19:07

                    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 !

                    L Offline
                    L Offline
                    LodiCode
                    wrote on 12 Mar 2017, 19:09 last edited by
                    #9

                    @Andy314 you is right my fault!

                    Let's code?

                    1 Reply Last reply
                    0

                    1/9

                    12 Mar 2017, 15:41

                    • Login

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