Results 1 to 5 of 5

Thread: QDataStream Inserting Spaces Between Characters

  1. #1
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDataStream Inserting Spaces Between Characters

    Hello all,

    I have an application that is receiving data over serial using QtSerialPort that is displaying data over a terminal (much like the examples in QtSerialPort if you are familiar with it). I also want to be able to save this data into a .txt file for further analysis. My problem is that I'm using QDataStream and it seems to be I am inserting extra characters somehow.

    For example, data coming over the terminal, and how it's supposed to
    1, 3, 2500000, 100000, 23000000

    but when I'm saving to a .txt file the data is saved as:
    > 1 , 3 , 2 5 0 0 0 0 0 , 1 0 0 0 0 0 , 2 3 0 0 0 0 0 0

    And yes, there's an extra ">" at the beginning


    Soooo here's my code. Here is to setup the save file:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. //Savefile setup
    4. if(ui->lineEdit_savefile->text().isEmpty()) on_pushButton_Browse_clicked(); //Check to make sure the user entered a file
    5. QString fileName = ui->lineEdit_savefile->text();
    6. if(!fileName.endsWith('.txt')){
    7. fileName.append(".txt");
    8. ui->lineEdit_savefile->setText(ui->lineEdit_savefile->text().append('.txt'));
    9. }
    10. saveFile.setFileName(fileName);
    11. if(!saveFile.open(QIODevice::WriteOnly | QIODevice::Text)){
    12. QMessageBox::information(this, tr("Unable to open file"),
    13. saveFile.errorString());
    14. return;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Here is how I write to the file:
    Qt Code:
    1. void MainWindow::saveData(const QByteArray &data){
    2. QDataStream out(&saveFile);
    3. out.setVersion(QDataStream::Qt_4_5);
    4. out << QString(data);
    5. }
    To copy to clipboard, switch view to plain text mode 


    And this is how the console application handles the data and prints it to the screen. It works correctly.
    Qt Code:
    1. void Console::putData(const QByteArray &data)
    2. {
    3. insertPlainText(QString(data));
    4.  
    5. QScrollBar *bar = verticalScrollBar();
    6. bar->setValue(bar->maximum());
    7. }
    To copy to clipboard, switch view to plain text mode 

    How do I get rid of these extra characters? Thank you very much in advance for your input(s)!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDataStream Inserting Spaces Between Characters

    You are asking QDataStream to serialise a QString full of Unicode characters into a binary format that it can restore later: it writes a four byte string length followed by that many two byte characters. QDataStream is not inserting extra anything, just enough to fulfil its purpose.

    If you want to write raw bytes to a file use the QIODevice::write() interface directly with your received QByteArray.

  3. The following user says thank you to ChrisW67 for this useful post:

    awpitt13 (21st September 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream Inserting Spaces Between Characters

    And if you want to write into a text file use QTextStream.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    awpitt13 (21st September 2013)

  6. #4
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream Inserting Spaces Between Characters

    Thanks, Chris... I was only able to get to this and try this today. I assumed to get this to work I had to use

    saveFile.write(data);

    This gets rid of the spaces in the text file but leaves a really good chunk of data out. I know I'm missing data since my terminal window that uses the same data array prints out all the data correctly.

    To anda_skoa, thank you for your reply, I'll try this and let you know how it goes.

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDataStream Inserting Spaces Between Characters

    The write() functions takes all the bytes in the array and puts them in the file (or whatever the QIODevice is). It does not interpret the bytes, trim the bytes, skip bytes or encode bytes. If the file does not actually contain the bytes then they were not in the array to start with (or there was insufficient space to write the file etc.). If you are writing bytes that might contain non-printable characters and subsequently inspect that with something expecting pure text then it may seem that things are missing when they are not. Exactly which way your result is "broken" for you we cannot tell.

Similar Threads

  1. QTableWidget spaces between column
    By evergreen in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2011, 10:27
  2. Spaces in QTextEdit
    By jgrauman in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2009, 18:28
  3. Spaces between lines in QTextEdit
    By troorl_ua in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 00:06
  4. inserting string > 127 characters to MS Access
    By jh in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2006, 17:11
  5. qmake INCLUDEPATH with spaces
    By bitChanger in forum Qt Programming
    Replies: 8
    Last Post: 28th April 2006, 05:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.