Results 1 to 13 of 13

Thread: Q Line edit problem

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

    Default Q Line edit problem

    I used Qtextstream to input line edit text to a text file, but the ui->lineEdit->text(), i update the QLINE EDIT and run qtextstream to input, but it only inputs the default value of line edit, like 70. whats the problem?? how can i take the current value not default.

  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: Q Line edit problem

    You can start by reading what you wrote here and then rewriting the question in a more understandable fashion.
    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
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Q Line edit problem

    Okay
    I got a line edit, and it has a default value, which I set it in the Design tab.
    Then I run the software, changed the line edit value.

    And I have a button when pressed, will take the line edit current value and save it into a text file.
    But every time I press the button, it saves the DEFAULT value, not the current value, and i use ui->lineEdit->text();

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q Line edit problem

    You must do something wrong in your code, setting the text in Line Edit and reading it are elementary tasks which do work.

    (1) Put the text in the Line Edit by ui->lineEdit->setText("some text"), the parameter is a QString.
    (2) Make sure the Line Edit displays "some text".
    (3) Read the text using ui->lineEdit->text(), the return value is a QString.
    (4) Make sure (using the debugger) that the returned value is "some text"
    (5) Store the value in a file.

    Post your code so that we can fix what is wrong. You need not post everything, only the part that relates to writing and reading Line Edit. Also post the corresponding part of your headers.

  5. #5
    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: Q Line edit problem

    ya, not some text but some integer/float value.

    i didn't use settext(), but changed the value in GUI

    this is my TESTING code :
    Qt Code:
    1. QFile file3("./Left.txt");
    2. file3.open(QIODevice::WriteOnly | QIODevice::Text);
    3.  
    4. QApplication::processEvents();
    5.  
    6. b = ui->lineEdit_8->text();
    7. QTextStream out(&file3);
    8. out << b;
    To copy to clipboard, switch view to plain text mode 

  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: Q Line edit problem

    You are most likely either:
    • not putting text into the widget lineEdit_8, or
    • manipulating a widget in one instance of the class, and reading/saving from another unmodified instance.


    The presence of line 4 is a fairly good sign that more than one thing is wrong with your design.

  7. #7
    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: Q Line edit problem

    I did put some number into line edit 8
    i got a function of line edit 8 text changed, but anyway, the CURRENT value/text cant be gotten from ->text();

  8. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q Line edit problem

    (1) Comment out line 4 (you should not need it in a single QApplication app).
    (2) Insert ui->lineEdit_8->setText("some text");
    (3) Put break at (current) line 8.
    (4) Debug.

    What is in b? If it is "some text" then lineEdit_8 isn't the widget where you are entering your text (see ChrisW67 comment above). Check your .ui file.

  9. #9
    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: Q Line edit problem

    weird thing is if i go to file explorer and delete the text file, (left.txt) and press the button to save. It works. It save the current value
    but if i just save or even use QFile to remove and Resize the text file to 0 it wont work.

    And if I save and the text file exist ( means i didn't delete the text file), the data/text in text file will be changed, not to current value but to DEFAULT value

    #so it seems is bcause i run mainwindow function from another class that caused this

    Qt Code:
    1. MainWindow main1;
    2. main1.Save1();
    3. // What to do??
    To copy to clipboard, switch view to plain text mode 
    Last edited by ttimt; 14th January 2014 at 09:10.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Q Line edit problem

    It really doesn't matter from where you call the function, but make sure you don't have a new instance of the class (see second point of ChrisW67's first comment)

    That code snippet looks like you are creating a new MainWindow instance and then call its Save1() method. That will obviously only save the default value, because that window has just been created and only contains the default values.

    I think there is no point in continuing with this thread without more code. This has turned to just repeatedly guessing.

    Cheers,
    _

  11. #11
    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: Q Line edit problem

    If you create a second instance of the same class, it has no relation to the first instance of the class. Each of them has its own separate unique lineEdit_8 object that has no relation to the other lineEdit_8 object. Thus if you modify the value in one of the object but read the value from the other object then you get that object's value (the default one) and not the one you modified.
    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.


  12. #12
    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: Q Line edit problem

    Not sure if this is a good approach but, i created global variable QString, and set them to the current value in the lineEdit textChanged function, and I save the global variable into the file, this way its working.

    But there's another problem, the above method only save CURRENT value, there are some line edit that I didnt change, means the text is still it's default text. By using the above method, it cant save default value to the file.

    Is there any way i can use lineedit->text() and get default and current value at the same time? Like updating the text()

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Q Line edit problem

    Why do you need a global variable?

    You have access to the line edit in your save method, right?

    Cheers,
    _

Similar Threads

  1. Problem>QT and Mini2440: Get text from Line edit
    By mrquicuong in forum Newbie
    Replies: 1
    Last Post: 26th November 2011, 13:26
  2. line edit mask problem
    By sudhansu in forum Qt Programming
    Replies: 3
    Last Post: 31st March 2010, 13:17
  3. Assert problem in line edit
    By grsandeep85 in forum Qt Programming
    Replies: 19
    Last Post: 28th July 2009, 05:30
  4. Problem updating Line edit from a thread
    By raghvendramisra in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 05:05
  5. Line Edit Controll problem
    By mansoorulhaq in forum Qt Programming
    Replies: 5
    Last Post: 31st October 2007, 08:33

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.