PDA

View Full Version : Q Line edit problem



ttimt
13th January 2014, 17:12
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.

wysota
13th January 2014, 17:55
You can start by reading what you wrote here and then rewriting the question in a more understandable fashion.

ttimt
14th January 2014, 06:04
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();
:confused::confused::confused:

Radek
14th January 2014, 06:26
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.

ttimt
14th January 2014, 06:50
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 :


QFile file3("./Left.txt");
file3.open(QIODevice::WriteOnly | QIODevice::Text);

QApplication::processEvents();

QString b;
b = ui->lineEdit_8->text();
QTextStream out(&file3);
out << b;

ChrisW67
14th January 2014, 07:26
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.

ttimt
14th January 2014, 07:44
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();

Radek
14th January 2014, 08:35
(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.

ttimt
14th January 2014, 08:43
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



MainWindow main1;
main1.Save1();
// What to do??

anda_skoa
14th January 2014, 09:25
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,
_

wysota
14th January 2014, 09:27
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.

ttimt
14th January 2014, 15:39
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. :confused:

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

anda_skoa
15th January 2014, 09:58
Why do you need a global variable?

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

Cheers,
_