Results 1 to 7 of 7

Thread: QTextStream, input and readLine()

  1. #1
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTextStream, input and readLine()

    Hi. I seem to have a problem where I dont understand the cause. I have read as much as I can trying to figure out what is my problem but alas, here I am humbly seeking assistance.

    I am working on a small and simple gui to generate plaintext config files that will be read by another program. I have output working fine but the problem arises when I try to input a QString that has spaces (a windows pathname to My Documents), my input is truncated at the first space in the string.

    My settings file looks like this (or very similar):

    server=example.com
    protocol=sftp
    port=23
    username=mark
    password=
    backupdir=c:\My Documents\backup
    firefox=1
    path=c:\Program Files\Firefox\firefox.exe
    I use QString::section() to get rid of the variable label which seems to work. Its just that pesky white space.

    Qt Code:
    1. //###########################
    2. void MainWindow::load()
    3. //###########################
    4. {
    5. QString str, chopped;
    6. int num;
    7.  
    8. QFile file("settings.cfg");
    9.  
    10. if (!file.open(QIODevice::ReadOnly))
    11. {
    12. cerr << "Cannot read file for loading: " << qPrintable(file.errorString()) << endl;
    13. loadDefaults();
    14. return;
    15. }
    16.  
    17. QTextStream input(&file);
    18.  
    19. // Server
    20. input >> str;
    21. chopped = str.section('=', 1);
    22. server_lineEdit->setText(chopped);
    23.  
    24. // Protocol
    25. input >> str;
    26. chopped = str.section('=', 1);
    27. if ( chopped == "sftp" )
    28. sftp_radioButton->setChecked(true);
    29. else
    30. ftp_radioButton->setChecked(true);
    31.  
    32. ...
    33.  
    34. // Backup Directory
    35. //input >> str;
    36. QString line = input.readLine();
    37. chopped = line.section('=', 1);
    38. backup_dir_lineEdit->setText(line);
    39.  
    40. // Firefox
    41. input >> str;
    42. chopped = str.section('=', 1);
    43.  
    44. if (chopped == QString::number(1))
    45. firefoxBox->setChecked(false);
    46. else
    47. firefoxBox->setChecked(true);
    48. // Path
    49. input >> str;
    50. //str = input.readLine();
    51.  
    52. chopped = str.section('=', 1);
    53. firefox_path_lineEdit->setText(chopped);
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 

    As you can see I have tried readLine() and a normal stream operators. The code in there now, the readLine() does not return anything to the lineEdit and if I switch it back to str << ... I get truncated path names. Please can someone point me in the direction of what I need. Thanks!

  2. #2
    Join Date
    Aug 2007
    Location
    Russia
    Posts
    19
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream, input and readLine()

    Try this:
    Qt Code:
    1. str = QString::fromLocal8Bit(file.readLine().data());
    2. chopped = str.mid(str.indexOf('=')+1);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextStream, input and readLine()

    Unfortunately that did not work. Even ignoring the chopped var and trying to load the QString str into the lineEdit didn't work. Thanks for the help though.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream, input and readLine()

    But can you read the entire line correctly from the stream?
    For example, can you read: "backupdir=c:\My Documents\backup"?

    Because there are other ways, for example QString::split("=");

    Regards

  5. #5
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextStream, input and readLine()

    Unfortunately the only time that anything is loaded into the QLineEdit is when I use the code input >> str;

    I tried Wizards suggestion without using a seperate or split function on the string but that method does not load the LineEdit with anything. What was forming to be a very simple program in Qt has turned quite frustrating as I do not know what else to try. Is there something about line endings that I am missing ? Maybe I need to output to the terminal what is getting loaded by the readLine() method.

  6. #6
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextStream, input and readLine()

    I believe I have isolated the problem. I use a QMessageBox to read back to me the QString after it is read from the stream. I used:

    Line being read from .txt: backupdir=C:/Documents and Settings/Mark/Sync

    1.) input >> str;

    2.) str = input.readLine();

    3.) str = QString::fromLocal8Bit(file.readLine().data());

    Only number 1 returned anything other than a null variable. The whole problem is it only returns C:/Documents.

  7. #7
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextStream, input and readLine()

    After a couple days I finally solved my problem. It seems that when reading a file you cant use a >> input stream operator and then use readLine(), you must use only one for each open stream. I switched all my input lines to readLine() and it worked.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.