Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

debug variable



  • Hello qt people :) ,

    i have a little pbl :'(
    i'don't understand why the qvector<qstring> is empty !
    if you have any idea

        QVector<QString> vector;
        vector << "observation" << "diag" << "ca" << "pa";
    
        QVector<QString> all_ligne,all_ligne1,all_ligne2,all_ligne3;
    
    for(int p=0;p<4;p++)
    {
    
        QFile file(path_app+"/report/"+vector.at(p)+".txt");
    
    
        QString ligne;
    
        file.resize(0); // to flush
    
        if (file.open(QIODevice::ReadWrite)) {
    
            QTextStream stream(&file);
    
    
            if(p==0)
            stream << ui->observation->toPlainText(); //qtextedit
            if(p==1)
            stream << ui->diag->toPlainText();
            if(p==2)
            stream << ui->ca->toPlainText();
            if(p==3)
            stream << ui->pa->toPlainText();
    
    
    
            while(!stream.atEnd()) {
    
                ligne = stream.readLine();
    
                if(p==0)
                all_ligne.append(ligne);
                if(p==1)
                all_ligne1.append(ligne);
                if(p==2)
                all_ligne2.append(ligne);
                if(p==3)
                all_ligne3.append(ligne);
    
    
                }
    
    
    
            file.flush();
    
    
            file.close();
    
    
    
        }
        else {
            QMessageBox::critical(this, tr("Error"), tr("Double ouverture"));
            return;
        }
    
    
    
    }
    
    qDebug() << all_ligne.size() << all_ligne;
    

    this code is in a qpushbutton so :
    first shot - QVector()
    second shot - 0 QVector("test")

    why in the first shot he not take the value ?


  • Lifetime Qt Champion

    @Albator said in debug variable:

    i'don't understand why the qvector<qstring> is empty !

    How do you know it is empty? So, what happens?
    Also, can you please post whole code and not just this snippet as it is not clear where vector is actually initialised.



  • @Albator said in debug variable:

    i'don't understand why the qvector<qstring> is empty !

    Perhaps you have to go to file begin after writing to be able to read it ;)
    Add something like this before to reading loop:

    stream.flush(); // to write all to the file
    stream.seek(0); // go to file begining
    while(!stream.atEnd()) {
    ...
    }
    


  • @KroMignon

    arf it was so simple, you just had to go back to the beginning of the doc...

    thank you :)


Log in to reply