简体   繁体   中英

How to set the QLineEdit with new line

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

文件输入输出

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);

//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM