Results 1 to 10 of 10

Thread: How to take user input in lineEdit and write that to file

  1. #1
    Join Date
    Dec 2012
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Exclamation How to take user input in lineEdit and write that to file

    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:
    Qt Code:
    1. void Write2(QString Filename2){
    2. QFile mfile2(Filename2);
    3. if (!mfile2.open(QIODevice::WriteOnly | QIODevice::Text))
    4. {
    5. qDebug() << "could not open file";
    6. return;
    7. }
    8.  
    9. QTextStream stream(&mfile2);
    10. stream >> ui->textEdit->text();
    11. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by wysota; 11th December 2012 at 11:05. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to take user input in lineEdit and write that to file

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to take user input in lineEdit and write that to file

    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:
    Qt Code:
    1. stream << textEdit->plainText();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2012
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to take user input in lineEdit and write that to file

    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!!!

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to take user input in lineEdit and write that to file

    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.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to take user input in lineEdit and write that to file

    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.

  7. #7
    Join Date
    Dec 2012
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to take user input in lineEdit and write that to file

    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!!!!!

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to take user input in lineEdit and write that to file

    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:
    Qt Code:
    1. void Write2(QString Filename2, QString textToWrite){
    2. //...rest of your code
    3. //
    4. stream << textToWrite;
    5. }
    To copy to clipboard, switch view to plain text mode 
    And you call that function from the slot connected with the clicked signal of your button and pass the text too.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to take user input in lineEdit and write that to file

    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.

    untitled.zip
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  10. #10
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to take user input in lineEdit and write that to file

    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......

Similar Threads

  1. hoe to write user input data into QTcpserver port
    By gauravg in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2012, 06:47
  2. How to input Chinese characters in LineEdit or TextEdit ?
    By behlkush in forum Qt Programming
    Replies: 2
    Last Post: 23rd May 2011, 13:26
  3. User Input using QString
    By kumarpraveen in forum Newbie
    Replies: 4
    Last Post: 9th July 2010, 08:49
  4. LineEdit for Hexadecimal input
    By mastupristi in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2010, 15:51
  5. read and write content of lineedit widget in Other Form
    By validator in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 16:07

Tags for this Thread

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.