Reputation: 503
Currently I am copying the text to a LineEdit and on click PushButton it will write the text to a file which is "data.txt". I have written a readfile()
which will read the text from data.txt and on click PushButton it should display the text in new line format at LineEdit.
Here is my code:
void MainWindow::writefile()
{
QString str = ui->lineEdit->text();
QString filename = "data.txt";
QFile file(filename);
file.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream out(&file);
out<<str<<endl;
file.close();
}
void MainWindow::readfile()
{
QString filename = "data.txt";
QFile file(filename);
file.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream in(&file);
QString str = in.readLine();
ui->lineEdit_2->setText(str);
file.close();
}
void MainWindow::on_pushButton_2_clicked()
{
readfile();
}
void MainWindow::on_pushButton_clicked()
{
writefile();
}
Please suggest how to separate those comma-separated strings and must display in new line format
Upvotes: 5
Views: 10354
Reputation: 35891
The documentation of QLineEdit
says:
A line edit allows the user to enter and edit a single line of plain text [...]
A related class is
QTextEdit
which allows multi-line, rich text editing.
Thus, you should use the QTextEdit
widget instead of QLineEdit
to allow multi-line text. It also has a setText
, so you can use it the same way.
To replace commas with new line characters use the replace
method:
// ...
QString str = in.readLine();
str = str.replace(",", "\n");
ui->textEdit_2->setText(str);
//...
Upvotes: 6