Write text file in Qthread
-
Hi,
And what exactly is your issue ?
I hope you are giving due credit to the people helping you when returning your assignments ;-)
-
Hi,
And what exactly is your issue ?
I hope you are giving due credit to the people helping you when returning your assignments ;-)
-
Hi,
And what exactly is your issue ?
I hope you are giving due credit to the people helping you when returning your assignments ;-)
@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 belowvoid 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.
-
@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 belowvoid 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.
@H-dragon
so, have you tried thisout << 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-dragon
so, have you tried thisout << 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@J-Hilk
Yes, I runout << timer->elapsed() << " " << location<<"\n";in the following 4 situations.- run in mainwindow.cpp just once -> run properly
- 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 }- run in the thread, but not in while loop -> GUI terminate. It doesn't work.
- run in while loop -> my goal, but doesn't work too
-
@J-Hilk
Yes, I runout << timer->elapsed() << " " << location<<"\n";in the following 4 situations.- run in mainwindow.cpp just once -> run properly
- 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 }- run in the thread, but not in while loop -> GUI terminate. It doesn't work.
- run in while loop -> my goal, but doesn't work too
@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-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; -
@J-Hilk
Thank you for suggestion. but it doesn't work. Shouldn't I use QTextsteram in the thread?@H-dragon in the case above, out is a QTextStream object, is it not ? (it has flush() as well)
-
@H-dragon in the case above, out is a QTextStream object, is it not ? (it has flush() as well)
-
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.