Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. "plain/text" encodedData and QDataStream to readable QString
Forum Updated to NodeBB v4.3 + New Features

"plain/text" encodedData and QDataStream to readable QString

Scheduled Pinned Locked Moved C++ Gurus
2 Posts 1 Posters 2.7k 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
    nandxor
    wrote on 3 Jul 2014, 12:23 last edited by
    #1

    I'd like to convert the following snippet to use "plain/text" mimetype without relying on QTextStream

    @
    bool DragDropListModel::dropMimeData(const QMimeData *data,
    Qt::DropAction action, int row, int column, const QModelIndex &parent)
    {
    ...
    QByteArray encodedData = data->data("application/vnd.text.list");
    QDataStream stream(&encodedData, QIODevice::ReadOnly);
    QStringList newItems;

    while (!stream.atEnd()) {
    QString text;
    stream >> text;
    newItems << text;
    }
    ...
    }
    @

    I tried to apply what said at http://stackoverflow.com/questions/19814193/qt-parse-string-of-undefined-size-from-a-binary-data-stream

    @
    char *ch;
    QFile file("file.dat");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);
    in >> ch;
    QString str(ch);
    @

    and http://www.qtcentre.org/threads/696-QDataStream-reading-into-QString

    @
    stream.setByteOrder( QDataStream::BigEndian);
    ...
    quint16 id;
    stream >> id; // First two bytes
    char* filename;
    stream >> filename; // String of undefined size
    QString file = QString::fromLatin1(filename);
    qDebug() << "output: " << file;
    newItems << file;
    delete[] filename; //cleanup
    @

    but I am always getting an empty string or 0.
    I am reading that "The only disadvantage to using QDataStream (over QTextStream) is that the resulting file is binary (i.e., not human readable)."
    Is there no way to convert QDataStream of plain text to readable QString?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nandxor
      wrote on 3 Jul 2014, 15:59 last edited by
      #2

      // drag&drop plain/text from outside the app is "abcdefg"

      @ while (!stream.atEnd()) {
      char* a;
      stream.readRawData(a, 7); // abcdefg length = 7
      QString string(a);
      qDebug() << "output " << string; // WORKING => abcdefg
      }@

      say I write the following code before the while loop:

      @ quint8 v;
      stream >> v;
      qDebug() << "output: " << v; // => 97 = "a"@

      which is equivalent to

      @ quint8 v;
      stream >> v;
      QString a;
      qDebug() << "output: " << static_cast<char>(v); // => "a"@

      and the while loops crashes:<br>
      error: Exception at 0x5dd02907, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)

      my plain/text string doesn't seem to have length bytes at the beginning.
      I could run it as it is but I am wondering if I could determine the length of the string in advance.
      Whenever I try to read from the stream through the << operator, I can't simply reset it to the beginning.

      1 Reply Last reply
      0

      1/2

      3 Jul 2014, 12:23

      • Login

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