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.

  • What is the question, what exactly does not work? - fghj
  • Format the code in question - paceholder
  • formatted. - timob256
  • @ timob256, try to reason logically. From the first QLineEdit text 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 variable flanech_x and the array of cells . 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 the cells contain a pointer (s) to a nonexistent QLineEdit . Check the code that forms the array of cells and assigns the value of the variable flanech_x . - alexis031182
  • alexis031182 I caught it in debugs, and here they are doing EVERYTHING, and then falling apart - timob256

1 answer 1

The code presented in the question, it is better to completely replace with something else that will be a little simpler.

 // Метод, выполняющий запись текстовой строки в файл. void MainWindow::writeText(const QString &fname, const QString &text) { QFile file(fname); if(file.open(QFile::WriteOnly|QFile::Text)) { QTextStream stream(&file); #ifdef Q_OS_WIN stream.setCodec("Windows-1251"); #endif stream << text << endl; file.close(); } } void MainWindow::on_pB_oform_otch_clicked() { // Записываем строку из первого QLineEdit. writeText("file_fam.txt", ui->lE_fam->text()); // Записываем строки из других QLineEdit в "cells". for(int i = 0; i < flanech_x; ++i) { const QString fname = QString("file_fam%1.txt").arg(i+1); writeText(fname, cells[i]->text()); } } 

Try not to use arrays without the necessary reason. In Qt, a QList usually used to store a list of pointers. The correctness of filling the array "cells" remains behind the frame, so also check the part of the program where this filling is performed.

  • Wu you, the cart is not there again. - timob256