I wrote this code:
void StartToSendCommand(QString fileName, QPlainTextEdit *textEdit)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTimer * inputTimer=new QTimer(textEdit);
QTextStream in(&file);
QString line;
while (!in.atEnd()) {
line = in.readLine();
if (line==""||line[0]=="#")
continue;
qDebug()<<line;
//TO DO: make also a waiting time between letters.
inputTimer->start(GWaitingTimeBetweenCommand);
QApplication::processEvents();
QThread::sleep(2);`
}
inputTimer->deleteLater();
SendCommandByUsb(fileName, line);
}
and I want to do that when its read from the file its also make a wait for 1 second between any letter. how can I make it?
If you want to wait for individual letters, you can also do this with a QTimer
interval. Once the condition is met, you can submit the file.
Here would be a small example, oriented towards a typewriter but has the same effect:
.h
....
private slots:
void typeWriter();
private:
QString line;
QTimer *timer;
....
.cpp
//constructor
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
line = "C:/.../readme.txt";
timer = new QTimer(this);
connect(timer,&QTimer::timeout, this, &MainWindow::typeWriter);
timer->start(1000);
}
void MainWindow::typeWriter()
{
static int counter = 0;
if(counter < line.length())
{
counter ++;
ui->label->setText(line.mid(0, counter));
//SendCommandByUsb(fileName, line.mid(0, counter));
timer->setInterval(1000);
qDebug() << counter << " : " << line.length() << " : " << line.mid(0, counter);
}
if(counter == line.length())
{
qDebug() << "end";
timer->stop();
}
}
the length of the string is compared with the counter. it is counted until it reaches the length of the string. the string method QString::mid can be thought of as a slice. it is sliced from index 0 (i.e. the beginning of the string) to the end, the counter takes care of that. The timer interval defines the interval between the next call.
Just for info (because that's how the question read): If you want to open a file and always want to wait a second for a letter to be read, you have to consider that you don't have a valid file path. Well that wouldn't make sense because you have to commit the file before anything is read at all.