Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Problem to reset QVector

    General and Desktop
    3
    9
    2135
    Loading More Posts
    • 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.
    • LodiCode
      LodiCode last edited by A Former User

      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 Reply Quote 0
      • ?
        A Former User last edited by

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

        LodiCode 1 Reply Last reply Reply Quote 2
        • LodiCode
          LodiCode @Guest last edited by

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

          Let's code?

          ? 1 Reply Last reply Reply Quote 0
          • ?
            A Former User @LodiCode last edited by

            @LodiCode Please show some code.

            LodiCode 1 Reply Last reply Reply Quote 1
            • LodiCode
              LodiCode @Guest last edited by

              @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 Reply Quote 0
              • ?
                A Former User last edited by

                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?

                LodiCode 1 Reply Last reply Reply Quote 1
                • LodiCode
                  LodiCode @Guest last edited by

                  @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 Reply Quote 0
                  • Andy314
                    Andy314 last edited by 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 !

                    LodiCode 1 Reply Last reply Reply Quote 2
                    • LodiCode
                      LodiCode @Andy314 last edited by

                      @Andy314 you is right my fault!

                      Let's code?

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post