PDA

View Full Version : QTextStream, input and readLine()



kramed
31st August 2007, 00:20
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.



//###########################
void MainWindow::load()
//###########################
{
QString str, chopped;
int num;

QFile file("settings.cfg");

if (!file.open(QIODevice::ReadOnly))
{
cerr << "Cannot read file for loading: " << qPrintable(file.errorString()) << endl;
loadDefaults();
return;
}

QTextStream input(&file);

// 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;
QString line = input.readLine();
chopped = line.section('=', 1);
backup_dir_lineEdit->setText(line);

// Firefox
input >> str;
chopped = str.section('=', 1);

if (chopped == QString::number(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!

Wizard
31st August 2007, 01:35
Try this:

str = QString::fromLocal8Bit(file.readLine().data());
chopped = str.mid(str.indexOf('=')+1);

kramed
31st August 2007, 02:53
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.

marcel
31st August 2007, 04:57
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

kramed
31st August 2007, 05:20
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.

kramed
31st August 2007, 06:26
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.

kramed
1st September 2007, 23:54
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.