user976369
user976369

Reputation: 1

Writing the user entered data, in a Qt4 UI,to a file

I created a UI using QtSDK and now i want to get the data entered by the user, in the UI, to a static file. How can I do this? For example, I tried:

ofstream myfile ("C:\\testcase.txt"); 
if (myfile.is_open()) {
    myfile << "ui->lineEdit->text()";
} else {
    cout << "Unable to open file";
}  

and it is printing the line within double quotes as it is in the file instead of printing the text entered in lineEdit and if i write

myfile << ui -> lineEdit -> text();

without the double quotes, the code shows the following error.

mainwindow.cpp:198: error: no match for 'operator<<' in 'myfile << QLineEdit::text() const()'

How should this be done?

Upvotes: 0

Views: 434

Answers (2)

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143061

You probably can't << QString directly, to std::ostream. Perhaps you need something like ui->lineEdit->text()() or convert it to some standard type otherwise, look up the docs.

Upvotes: 0

pnezis
pnezis

Reputation: 12321

By using double quotes you actually provide a string. The expression within the quotes is not evaluated.

You should use a QTextStream in order to write to a file.

QFile file("myfile.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    return;

QTextStream filestream(&file);
filestream << ui->lineEdit->text();

Qt Documentation is great, so check it for more details

Upvotes: 1

Related Questions