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. How to convert QList<QByteArray> to QStringList ?
Forum Updated to NodeBB v4.3 + New Features

How to convert QList<QByteArray> to QStringList ?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 7.3k 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    How to convert QList<QByteArray> to QStringList ?
    I want to use QStringList::filter() function.

    https://github.com/sonichy

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @sonichy

      How to convert QList<QByteArray> to QStringList ?

      It depends on what character encoding is used in the QByteArrays, but assuming they use the local system's 8-bit encoding (for example), you can do it like:

      QStringList toStringList(const QList<QByteArray> list) {
          QStringList strings;
          foreach (const QByteArray &item, list) {
              strings.append(QString::fromLocal8Bit(item)); // Assuming local 8-bit.
          }
          return strings;
      }
      

      Of course, you can replace the QString::fromLocal8Bit() with whatever makes sense for your encoding.

      I want to use QStringList::filter() function.

      The above will work. But if you're only interested in the QStringList::filter() overload that takes a QString pattern, and you want to be case sesnsitive (the default), then you can avoid the string conversions by rolling your own "filtered" function like:

      QList<QByteArray> filtered(const QList<QByteArray> list, const QByteArray &bytes) {
          QList<QByteArray> result;
          foreach (const QByteArray &item, list) {
              if (item.contains(bytes))
                  result.append(item);
          }
          return result;
      }
      

      An example of using both the above functions:

          const QList<QByteArray> list = { "a", "b", "c", "abc", "123", "123abc" };
          const QStringList strings = toStringList(list);
      
          qDebug() << "list:            " << list;
          qDebug() << "strings:         " << strings;
          qDebug() << "strings-filtered:" << strings.filter(QLatin1String("b"));
          qDebug() << "list-filtered:   " << filtered(list, "b");
      

      Outputs:

      list:             ("a", "b", "c", "abc", "123", "123abc")
      strings:          ("a", "b", "c", "abc", "123", "123abc")
      strings-filtered: ("b", "abc", "123abc")
      list-filtered:    ("b", "abc", "123abc")
      

      Cheers.

      1 Reply Last reply
      10

      • Login

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