The button, when clicked, creates files by the number of lineEdit, and is filled with data lying in lineEdit.
void MainWindow::on_pB_oform_otch_clicked() { QFile file; // вначале можно не указывать файл QTextStream stream; int x=0; str_fam.append(QString("%1 %3").arg(trUtf8("file_fam")).arg(trUtf8(".txt"))); file.setFileName(str_fam); file.open(QIODevice::WriteOnly); stream.setDevice(&file); // указываем, что работать будем с нашим файлом stream << ui->lE_fam->text(); file.close(); // закрываем файл str_fam.clear(); if(flanech_x>0) { int flanech_x_1 = flanech_x; int r=0; for ( r ; r<flanech_x_1 ; r++) { str_fam.append(QString("%1 %2 %3").arg(trUtf8("file_fam")).arg(r+1).arg(trUtf8(".txt"))); file.setFileName(str_fam); // указываем файл file.open(QIODevice::WriteOnly); // открываем его для записи stream.setDevice(&file); // указываем, что работать будем с нашим файлом flanech_x = r; stream << cells[flanech_x]->text(); file.close(); // закрываем файл str_fam.clear(); } } } This code works fine with one (the last LineEdit), but how I will look at the array does not cope. How to make so that this code would lay down the data from
cells[flanech_x] in file
str_fam // -------------------------------------------
I used the code that gave me alexis031182. The result is something like this:
void MainWindow::writeText(const QString &fname, const QString &text) { qDebug() << "qwerty"; QFile file(fname); if(file.open(QFile::WriteOnly|QFile::Text)) { QTextStream stream(&file); qDebug() << "qwerty11"; #ifdef Q_OS_WIN stream.setCodec("Windows-1251"); #endif qDebug() << "qwerty22"; stream << text << endl; qDebug() << "qwerty33"; file.close(); } } void MainWindow::on_pB_oform_otch_clicked() { qDebug() << "trylala"; // Записываем строку из первого QLineEdit. writeText("file_fam.txt", ui->lE_fam->text()); qDebug() << "trylala11"; // Записываем строки из других QLineEdit в "cells". for(int i = 0; i < flanech_x; ++i) { qDebug() << "trylala22"; const QString fname = QString("file_fam%1.txt").arg(i+1); qDebug() << "trylala33"; writeText(fname, cells[i]->text()); } } The first file is created "file_fam.txt" and a line is inserted into it (all OK). BUT, then oh, even though the whole code passes, all qDebug () work. But for some reason the program is falling apart (-_-) \, and the file_fam1 .txt files (and so on up to 5) are not even created. Since I haven’t learned how to use debater in Qt (I don’t have it), I don’t even know what to do; _ ;.
If the variable has a value greater than the number of elements in the array, this will lead to an error. That is what was fixed, earned.
QLineEdittext is successfully retrieved and inserted into the file. So the recording mechanism is working. Then the cycle. The first thing you need to pay attention to is external with respect to the method resources, namely the variableflanech_xand the array ofcells. If the variable has a value greater than the number of elements in the array, this will lead to an error. Also, an error will occur if thecellscontain a pointer (s) to a nonexistentQLineEdit. Check the code that forms the array ofcellsand assigns the value of the variableflanech_x. - alexis031182