PDA

View Full Version : How to take user input in lineEdit and write that to file



drshonrscott
11th December 2012, 05:07
Hello QT Experts :)
I have searched and searched and searched, I know you hear that from everyone. Please just point me in the right direction and I will leave you alone :)

I have form, it has a textEdit on it labeled textEdit, among other things such as a couple of spin boxes and a lineEdit as well. The user can enter text into the lineEdit box and change the values of the spinbox and click a push button, when clicked it displays the values into textEdit. At the same time it will create a file in c:\ and write "You did something" into the file.

What I can't find is how to take the values of ANY of those form elements such as textEdit and write that value to the file. I know how to write to file I know how to write the value to the textEdit I just can't seem to find an example of how to one into the other.

I have a header file called mywrite.h it has all my void write () in it that create the file and write the information to file. Within this file I have the following:

void Write2(QString Filename2){
QFile mfile2(Filename2);
if (!mfile2.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "could not open file";
return;
}

QTextStream stream(&mfile2);
stream >> ui->textEdit->text();
}

Instead of the word stream I have tried "out" and that doesn't make any difference I get the following three errors:
C:\QTDEVDAY\1262012\MyFirstApp\myqtapp.cpp:72: error: C2065: 'ui' : undeclared identifier
C:\QTDEVDAY\1262012\MyFirstApp\myqtapp.cpp:72: error: C2227: left of '->textEdit' must point to class/struct/union/generic type
type is ''unknown-type''
C:\QTDEVDAY\1262012\MyFirstApp\myqtapp.cpp:72: error: C2227: left of '->text' must point to class/struct/union/generic type

I am sure it is something simple, like needing a signal/slot of some sort or something, or just a simple syntax change, I just don't know what that would be and can't seem to find an example.

If you can just simple past a link to an exact example of how to write the ui lineEdit or textEdit to a file that would be great, that is all I really want to do. This is a test example for me, I have a form with 20 lineEdits on it that I want to write to a file when button is pushed, that is really all I need to do.

thank you for your help in advance!!

Doc

wysota
11th December 2012, 10:09
It seems your "Write2" is a standalone function. It has no knowledge of the "ui" member of some class you have. Just put that Write2 function into the class (making it a member method of the class) and you'll be able to access the "ui" object. Also note there is not text() method in QTextEdit, there is QTextEdit::plainText.

This is all basic C++, nothing Qt related.

ChrisW67
12th December 2012, 08:48
Line 10 of your listing is trying to read from the stream into "ui->textEdit->text()", which cannot be used as the target of such a thing and would fail to compile even if "ui" did exist. Perhaps you meant:


stream << textEdit->plainText();

drshonrscott
16th December 2012, 02:26
wysota -- I can add it to the class and get the same error even changing it to plainText()
I get the same error either way any other thoughts?

Chris -- I get the same errors as above with utilizing your line of code that you sent me

Any other thoughts guys?

Thank you very much for the input!!!

Zlatomir
16th December 2012, 09:41
Have you tried combining both answers?
so: stream << ui->textEdit->plainText(); // and obviously with the function Write2 member in the same class that has the ui member.

ChrisW67
17th December 2012, 01:44
My line of code address one of the several errors in your code snippet. Wysota also points out a solution to the undefined "ui" issue. As Zlatomir says you have to combine a few things.

drshonrscott
17th December 2012, 02:45
I really appreciate all the input it is more confusing than helpful only due to my lack of knowledge in the use of the terms.

One last question and the thread can be deleted as I don't feel this is useful to anyone else due to the simplistic nature of what I am asking.

With that said:
Is there some place that I can follow an example of the following:
-- Create a form with 1 textbox called lineEdit and one push button call pushbutton1
-- User will write text into the text box and press the 1 button
-- When the button is pressed, it will create a file in "c:/" and write the text entered in the text box to the file

I believe once I have it doing this, I can do the rest based on this example as I can actually understand the relationship to each other and how it works. Even with the use of signal and slots as the answer.

I have searched the Internet and there is just not any resources that I have found that show you how to write the value of a "ui" element to a file. I can write a file all day long with the out function even with the above code it works great, it is when I try to take the text entered in the text box and then write that to file that I fail miserably.

If one of you or anyone can you write that example (yes asking a lot) or just give me a link to the example already done. The help in QT is excellent, BUT this example in help demos or on the welcome screen does not exist. I don't know if it because writing to file is just archaic or what. I have thought about just writing it to an array, but either one at this point are not easy for me to write.

If you do not want to take the time to do this I fully respect and understand as your time is very valuable.

Thank you all for the help!!!!!

Zlatomir
17th December 2012, 05:02
I don't think that there is an example for your specific need, and even if it is it won't help you that much - because you don't learn that much when you copy/paste code from an example - so i suggest you create a simple project and if you don't succeed in making it work you can post it here and we will help you understand what have you done wrong.

My guess right now is that you put Write2 function in a different class than the class that has the user interface (and the ui pointer) - this is just a guess, so you need to show us more code and tell the errors for us to help you further - another solution might be to pass an extra QString parameter to the function, so that you won't need the ui->textedit there, something like:

void Write2(QString Filename2, QString textToWrite){
//...rest of your code
//
stream << textToWrite;
}
And you call that function from the slot connected with the clicked signal of your button and pass the text too.

ChrisW67
17th December 2012, 05:15
This is a generic project with the addition of a single slot in mainwindow.cpp. It demonstrates how to find a "correct" place to save user files, how to access the text of a line edit or text edit, and how to put that text in a file.

8506

ttimt
17th December 2012, 13:20
From the code you posted on #1

You need to add the ui header to your mywrite.h file
#include <ui_mywrite.h>

*and also stream << ui->textEdit......