Reading strings from a QStringList to send over serial
-
I created a push button that when clicked it should send a list of commands over serial that complete the action of the button. I have my list of strings defined in my mainwindow.h file as follows:
QStringList database_dump = { "STRING1", "STRING2", "STRING3.", "STRING4", "STRING5", "STRING6", "STRING7" };
From my button click signal I want to extract each string and send it over to my "write_to_port" function as seen below:
void MainWindow::on_dump_database_clicked() { QString * file_name; QByteArray key_value; while(!database_dump.isEmpty()) { file_name = new QString(database_dump.takeFirst()); key_value = file_name->toLatin1(); key_value.append(NEW_LINE_CR); write_to_port(key_value); } }
After each string there needs to be a simulated enter key as shown.
The problem is; the .takefirst() member takes the string and removes it from the list. The first time I click the button it works perfectly. However the second time it does nothing because the QStringList is empty. How do I reset the list of strings after each time the button is clicked. Thanks in advance.
Nicholas
-
Why not simply iterator of the QStringList instead removing the items?
for(const QString &str : database_dump) { QByteArray key_value = str.toLatin1(); key_value.append(NEW_LINE_CR); write_to_port(key_value); }
-
@Christian-Ehrlicher I already got an answer. It is similar to your solution:
void MainWindow::on_dump_database_clicked() { foreach(const QString &file_name, database_dump) { QByteArray key_value(file_name.toLatin1()); key_value.append(NEW_LINE_CR); ui->terminalTxtBox->write_to_port(key_value); } }
Thanks for the help.
-
Please mark the thread as solved when you found a solution :)