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

Absurd QT File Handling Issue(QFile)



  • I am relatively new to QT and I tried converting a C++ terminal application to QT widget application. In my code, QFile is behaving absurdly in one function while it works fine in another, although both codes are similar. Here's the issue

    QString df_name;
    void MainWindow::on_choose_filed_clicked(){
        QString filter = "Text File (*.txt) ;; PDF File (*.pdf)";
        df_name = QFileDialog::getOpenFileName(this, "choose a file", QDir::homePath(), filter);
        ui->show_chosen->setText(df_name);
        QMessageBox::information(this, "Chosen File", df_name);
        QFile source_file(df_name);
        if(! source_file.open( QFile::ReadOnly )){
            QMessageBox::warning(this, "Error", "Cannot Open file");
        }
    }
    

    I then want to pass the df_name into another event when a button is clicked and read data from it. I do it like

    QString msg;
    void MainWindow::on_decode_now_clicked()
    {   ui->show_chosen->setText("begining of function");
        QFile source_file(df_name);
        QTextStream in(&source_file);
        QString ans{""};
        string out="";
        ui->show_chosen->setText("mid of function");
    //    while(!source_file.atEnd()){
    //        QString msg = in.readLine();
    //        string smsg= msg.toLocal8Bit().constData();
    //        out =  decode(smsg);
    //        QString qout= QString::fromUtf8(out.data(), int(out.size()));
    //       // ui->decoded->appendPlainText("qout");
    //    }
         msg= in.readAll();
        string smsg= msg.toStdString();
        out =  decode(smsg);
        QString qout= QString::fromUtf8(out.data(), int(out.size()));
        ui->decoded->appendPlainText(msg);
        ui->show_chosen->setText("end of function");
        source_file.flush();
        source_file.close();
    }
    

    Both the commented out code and uncommented code,don't work. On running the application and clicking the button, my application crashes. alt text
    decode(std::string s) is a function that returns std:: string.
    The same file handling code works when I use it in

    QString src;
    void MainWindow::on_choose_file_clicked()
    {
        QString filter = "Text File (*.txt) ;; PDF File (*.pdf) ;; Cpp File (*.cpp)";
        QString file_name = QFileDialog::getOpenFileName(this, "choose a file", QDir::homePath(), filter);
        ui->chosen_edit->setText(file_name);
        QMessageBox::information(this, "Chosen File", file_name);
        QFile source_file(file_name);
        if(! source_file.open( QFile::ReadOnly )){
            QMessageBox::warning(this, "Error", "Cannot Open file");
        }
        QTextStream in(&source_file);
        src = in.readAll();
        source_file.flush();
        source_file.close();
        //ui->dest_name->setText(src);
    }
    

    I have checked several sources but none of them worked for me. Please let me know if I'm missing on something.


  • Lifetime Qt Champion

    @Sabhya-Sood said in Absurd QT File Handling Issue(QFile):

    Please let me know if I'm missing on something.

    You should open a file before trying to read from it (but that's also true for plain c(++) file handling). And no you don't open the file object before trying to read it - c++ basics are missing here I would guess.



  • @Sabhya-Sood
    @Christian-Ehrlicher has just posted ahead of me: in your working cases you call QFile::open(), in your non-working case you do not. QFile does not do "absurd" file handling, if you don't open the file it won't behave as you are expecting....



  • Hi @Christian-Ehrlicher @JonB I tried what you told me and added to the

    if(! source_file.open( QFile::ReadOnly )){
           QMessageBox::warning(this, "Error", "Cannot Open file");
       }
    

    to

    void MainWindow::on_decode_now_clicked()
    

    before opening QTextStream
    but the application still crashes. Sorry if I'm still missing on something but could you help me out?


  • Lifetime Qt Champion

    Use a debugger to see where and why it crashes.



  • @Christian-Ehrlicher it pops a segmentation fault error
    "The inferior stopped because it received a signal from the operating system.

    Signal name :
    SIGSEGV
    Signal meaning :
    Segmentation fault"

    The decode() function logic works well on terminal though.


  • Lifetime Qt Champion

    Use a debugger ...


  • Moderators

    @Sabhya-Sood and what does the stack trace say?
    what was the last line called from your own code?



  • Hi @J-Hilk , it says "ReturnHr(2) tid(3e6c) 80010117 Call context cannot be accessed after call completed."
    I figured out that perhaps .readAll() function doesn't account for multi line strings and that's why popped a Segmentation error, because it did work when I tried to read a file with no line breaks.



  • @Sabhya-Sood

    1. Compile for debug.
    2. Execute by using the Debug button to run application.
    3. Make it hit the SEGV.
    4. At this point the debugger will "jump in" and break.
    5. Find the Stack trace window.
    6. You see a series of numbered "frames". Look back through the frames from where the SEGV happened till you see a line in your own code.
    7. Click on that frame line. Look at the statement and your local variables to see what's wrong. In all likelihood, a nullptr.

    If you really get stuck, post a readable screenshot. Unless you are looking in the right window pane you won't get anywhere.



  • @JonB Thanks for your insight. I did as you suggested and found one of my local variable's address a nullptr(though not sure where it came from). I changed a few lines of code and apparently, my code works fine now. @Christian-Ehrlicher @J-Hilk thanks!