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):
Quote:
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.
Code:
//###########################
void MainWindow::load()
//###########################
{
int num;
QFile file("settings.cfg");
{
cerr << "Cannot read file for loading: " << qPrintable(file.errorString()) << endl;
loadDefaults();
return;
}
// Server
input >> str;
chopped = str.section('=', 1);
server_lineEdit->setText(chopped);
// Protocol
input >> str;
chopped = str.section('=', 1);
if ( chopped == "sftp" )
sftp_radioButton->setChecked(true);
else
ftp_radioButton->setChecked(true);
...
// Backup Directory
//input >> str;
chopped = line.section('=', 1);
backup_dir_lineEdit->setText(line);
// Firefox
input >> str;
chopped = str.section('=', 1);
firefoxBox->setChecked(false);
else
firefoxBox->setChecked(true);
// Path
input >> str;
//str = input.readLine();
chopped = str.section('=', 1);
firefox_path_lineEdit->setText(chopped);
}
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!
Re: QTextStream, input and readLine()
Try this:
Code:
str
= QString::fromLocal8Bit(file.
readLine().
data());
chopped = str.mid(str.indexOf('=')+1);
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.
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
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.
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.
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.