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. Reading strings from a QStringList to send over serial
Forum Updated to NodeBB v4.3 + New Features

Reading strings from a QStringList to send over serial

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 476 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.
  • N Offline
    N Offline
    ndijkhoffz
    wrote on last edited by ndijkhoffz
    #1

    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

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

      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);
      }
      

      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
      4
      • N Offline
        N Offline
        ndijkhoffz
        wrote on last edited by
        #3

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

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

          Please mark the thread as solved when you found a solution :)

          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
          0

          • Login

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