Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Absurd QT File Handling Issue(QFile)

Absurd QT File Handling Issue(QFile)

Scheduled Pinned Locked Moved Solved C++ Gurus
qfiledialogqfileqt5.6c++ qtfile read
11 Posts 4 Posters 2.5k 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.
  • S Offline
    S Offline
    Sabhya Sood
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      5
      • S Sabhya Sood

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

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

        1 Reply Last reply
        3
        • S Offline
          S Offline
          Sabhya Sood
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Use a debugger to see where and why it crashes.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • S Offline
              S Offline
              Sabhya Sood
              wrote on last edited by
              #6

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

              J.HilkJ 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Use a debugger ...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                3
                • S Sabhya Sood

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

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

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


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    Sabhya Sood
                    wrote on last edited by
                    #9

                    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.

                    JonBJ 1 Reply Last reply
                    0
                    • S Sabhya Sood

                      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.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      8
                      • S Offline
                        S Offline
                        Sabhya Sood
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        3

                        • Login

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