Problem to reset QVector



  • 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...



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



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



  • @LodiCode Please show some code.



  • @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
    
    


  • 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?



  • @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!



  • 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 !



  • @Andy314 you is right my fault!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.