Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Write text file in Qthread
Forum Updated to NodeBB v4.3 + New Features

Write text file in Qthread

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 1.0k Views 1 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    And what exactly is your issue ?

    I hope you are giving due credit to the people helping you when returning your assignments ;-)

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    JonBJ H 2 Replies Last reply
    3
    • SGaistS SGaist

      Hi,

      And what exactly is your issue ?

      I hope you are giving due credit to the people helping you when returning your assignments ;-)

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

      @SGaist said in Write text file in Qthread:

      I hope you are giving due credit to the people helping you when returning your assignments ;-)

      LOL!

      1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        And what exactly is your issue ?

        I hope you are giving due credit to the people helping you when returning your assignments ;-)

        H Offline
        H Offline
        H.dragon
        wrote on last edited by
        #4

        @SGaist
        Hi, My issue is when I put the opening and writing file code, the while loop doesn't run.
        My original code is like below

        void Position::measureLocation1(MDCE* pcom)
        {
            double location;
            char record[] = "X_enc_pos";
            while (1)
            {
                GetVarN(record, &location, 0, pcom); //this function returns double value to location.
                emit location_changed1(QString::number(location));
                if (QThread::currentThread()->isInterruptionRequested()) return;
            }
        }
        

        It emits signals repeatedly in the thread properly.

        I expect the code runs through the following process when I changed codes

        create experiment.txt
        in while loop
        {
        write elapsed time and the value of the position to experiment.txt
        emit signal
        }
        when I give interruption, stop the while loop.

        I already mentioned to the professor that thanks to QT forum, I found breakthrough in my GUI work :) I really appreciate it.

        J.HilkJ 1 Reply Last reply
        0
        • H H.dragon

          @SGaist
          Hi, My issue is when I put the opening and writing file code, the while loop doesn't run.
          My original code is like below

          void Position::measureLocation1(MDCE* pcom)
          {
              double location;
              char record[] = "X_enc_pos";
              while (1)
              {
                  GetVarN(record, &location, 0, pcom); //this function returns double value to location.
                  emit location_changed1(QString::number(location));
                  if (QThread::currentThread()->isInterruptionRequested()) return;
              }
          }
          

          It emits signals repeatedly in the thread properly.

          I expect the code runs through the following process when I changed codes

          create experiment.txt
          in while loop
          {
          write elapsed time and the value of the position to experiment.txt
          emit signal
          }
          when I give interruption, stop the while loop.

          I already mentioned to the professor that thanks to QT forum, I found breakthrough in my GUI work :) I really appreciate it.

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

          @H-dragon
          so, have you tried this

          out << timer->elapsed() << " " << location<<"\n";

          without thread and while loop and everything?
          depending on what it is, QTextStream may not be able to interpret that variables to write them to file


          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.

          H 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @H-dragon
            so, have you tried this

            out << timer->elapsed() << " " << location<<"\n";

            without thread and while loop and everything?
            depending on what it is, QTextStream may not be able to interpret that variables to write them to file

            H Offline
            H Offline
            H.dragon
            wrote on last edited by
            #6

            @J-Hilk
            Yes, I run out << timer->elapsed() << " " << location<<"\n"; in the following 4 situations.

            1. run in mainwindow.cpp just once -> run properly
            2. use QTextStream as parameter -> compile error
            void MainWindow::data()
            {
                QFile file("C:/Users/Sonic/experiment.txt");
                if (!file.open(QFile::WriteOnly | QFile::Text))
                {
                    return;
                }
                QTextStream out(&file);
                thread->startThread1(motor, ui->statusHeader, out); ///QTextStream as parameter
            }
            
            1. run in the thread, but not in while loop -> GUI terminate. It doesn't work.
            2. run in while loop -> my goal, but doesn't work too
            J.HilkJ 1 Reply Last reply
            0
            • H H.dragon

              @J-Hilk
              Yes, I run out << timer->elapsed() << " " << location<<"\n"; in the following 4 situations.

              1. run in mainwindow.cpp just once -> run properly
              2. use QTextStream as parameter -> compile error
              void MainWindow::data()
              {
                  QFile file("C:/Users/Sonic/experiment.txt");
                  if (!file.open(QFile::WriteOnly | QFile::Text))
                  {
                      return;
                  }
                  QTextStream out(&file);
                  thread->startThread1(motor, ui->statusHeader, out); ///QTextStream as parameter
              }
              
              1. run in the thread, but not in while loop -> GUI terminate. It doesn't work.
              2. run in while loop -> my goal, but doesn't work too
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              @H-dragon

              If it works without the thread and the while loop, then my guess is, you're overwhelming the OS's file IO system or QTextStream. The write attempt probably happens a couple hundred times per second.

              Try to explicitly flush, after writing

              GetVarN(record, &location, 0, pcom); //this function returns double value to location.
                      out << timer->elapsed() << " " << location<<"\n"; // write time and location value to experiment.txt
                      out.flush();
                      emit location_changed1(QString::number(location));
                      if (QThread::currentThread()->isInterruptionRequested()) return;
              

              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.

              H 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @H-dragon

                If it works without the thread and the while loop, then my guess is, you're overwhelming the OS's file IO system or QTextStream. The write attempt probably happens a couple hundred times per second.

                Try to explicitly flush, after writing

                GetVarN(record, &location, 0, pcom); //this function returns double value to location.
                        out << timer->elapsed() << " " << location<<"\n"; // write time and location value to experiment.txt
                        out.flush();
                        emit location_changed1(QString::number(location));
                        if (QThread::currentThread()->isInterruptionRequested()) return;
                
                H Offline
                H Offline
                H.dragon
                wrote on last edited by H.dragon
                #8

                @J-Hilk
                Thank you for suggestion. but it doesn't work. Shouldn't I use QTextsteram in the thread?

                J.HilkJ 1 Reply Last reply
                0
                • H H.dragon

                  @J-Hilk
                  Thank you for suggestion. but it doesn't work. Shouldn't I use QTextsteram in the thread?

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

                  @H-dragon in the case above, out is a QTextStream object, is it not ? (it has flush() as well)


                  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.

                  H 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @H-dragon in the case above, out is a QTextStream object, is it not ? (it has flush() as well)

                    H Offline
                    H Offline
                    H.dragon
                    wrote on last edited by H.dragon
                    #10

                    @J-Hilk
                    yes. out is QTextStream object as you said. there is not an error but when I activate the function, my GUI just terminate

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      khryleption
                      wrote on last edited by khryleption
                      #11

                      Check with your debugger if the QTextBrowser is not accessed from the second thread. All GUI actions must be performed from the main thread. it's forbidden to access GUI from another thread.

                      1 Reply Last reply
                      0

                      • Login

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