简体   繁体   中英

Qt : Write in file

I'm learning how QT is working. I have this little code :

QApplication app(argc, argv);
QWidget fenetre;
fenetre.setFixedWidth(400);
fenetre.setFixedHeight(400);

QPushButton *bouton = new QPushButton("Quit", &fenetre);
bouton->setFixedHeight(50);
bouton->setFixedWidth(100);
bouton->move(170,310);


QLabel *label = new QLabel("Something", &fenetre);
label->move(30,200);
QLineEdit *line = new QLineEdit(&fenetre);
line->move(200,200);

QWidget::connect(bouton, SIGNAL(clicked()),qApp, SLOT(quit()));

fenetre.show();

I'm wondering how could I put the content of QLineEdit label in a file (.txt) continuously. The goal is to keep what the user put in the QLineEdit box in a text file when the program is finished.

Thanks

Use QFile and QTextStream to write to a file, when QLineEdit::textChanged signal is emitted as follows:

QObject::connect(line, &QLineEdit::textChanged, [](const QString& text) {
    QFile file("output.txt");

    if(file.open(QFile::WriteOnly | QFile::Text)) {
        QTextStream stream(&file);
        stream << text;
        file.close();
    }
});

The code above will connect a lambda expression to the QLineEdit::textChanged signal. The text argument of that lambda will be the changed content of your line . The code block inside the lambda simply rewrites the "output.txt" with the received text .

You can use QFile to Read/Write data in files text/binary format.

Button Press to save data:

void MainWindow::on_btnSaveData_clicked()
{
    writeDataInFile(ui->lineEdit->text());
}

Function Save Data:

void MainWindow::writeDataInFile(QString data)
{
    QString filePath = "D:/Your Path";
    QFile writeFile(filePath);
    if(!writeFile.open(QFile::Append | QFile::Text))
    {
        // Can't Open File.
    }
    else
    {
        if(data.length() > 0)
        {
            QTextStream in(&writeFile);
            in << data << "\r";
        }
    }
    writeFile.close();
}

This function gets a QString , opens a file ( filePath ) and appends data at the end of your file.

You can modify function and send QByteArray to the function or a Vector.

I'm wondering how could I put the content of QLineEdit label in a file (.txt) continuously . The goal is to keep what the user put in the QLineEdit box in a text file when the program is finished .

I can interpret these words as either you probably want to save QLineEdit content when your application is about to close or you want to save it "on a fly" , immidiately as user types something to QLineEdit . In both cases you can use some save() function like this:

void save(const QString &text)
{
    QFile f("out.txt");
    bool ok = f.open(QIODevice::WriteOnly);

    if(!ok) {
        qDebug() << "open file error!";
        return;
    }

    QTextStream stream(&f);
    stream << text;
    f.close();
}

and then apply one of these connect functions in your main :

// save on type
QObject::connect(line, &QLineEdit::textChanged, [line](const QString &text) {
    save(text);
});

// save on application close.
QObject::connect(&app, &QApplication::aboutToQuit, [line]() {
    save(line->text());
});

It is enough to use one of these connect s depending on your needs.

Note: &QApplication::aboutToQuit signal is emmitted when your application is closing either by pressing standard windows "Close" button or by pressing your custom "Quit" button.

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