There is a directory structure of the form dir/subdir[n]/file[m] . You must get the dir directory name, all subdir directory names, and count the number of files in the subdir directories. The collected information must be output to QTableWidget in the format:

 DIR| SUBDIR | count_file_SUBDIR ab 5 ac 6 ar 7 

At the moment, you can only see how many files are in the DIR directory (i.e., bypass the entire tree and return the number of files).

The problem is to bypass the subdirectories and display information about them.

 void MainWindow::on_btn_download_clicked(bool) { QString src = QFileDialog::getExistingDirectory(); std::string dir; std::string subdir = "SUBDIR";//заглушка int cf = 0;//число файлов в каталоге download_key(src, serial_number, cf); QString ck = QString::number(cf); QString sn = QString::fromStdString(dir); QString tp = QString::fromStdString(subdir); QTableWidgetItem* newItem1 = new QTableWidgetItem(tr("%1").arg(sn)); QTableWidgetItem* newItem2 = new QTableWidgetItem(tr("%1").arg(ck)); QTableWidgetItem* newItem5 = new QTableWidgetItem(tr("%1").arg(tp)); QTableWidgetItem* newItem3 = new QTableWidgetItem(tr("%1").arg(sn)); QTableWidgetItem* newItem4 = new QTableWidgetItem(tr("%1").arg(ck)); QTableWidgetItem* newItem6 = new QTableWidgetItem(tr("%1").arg(tp)); qDebug() << "KEY" << ck << sn << tp << t_col << t_row; ui->key_tableView->setRowCount(t_row + 1); ui->key_tableView_2->setRowCount(t_row + 1); ui->key_tableView->setItem(t_row, t_col, newItem1); ui->key_tableView_2->setItem(t_row, t_col, newItem3); t_col = 1; ui->key_tableView->setItem(t_row, t_col, newItem5); ui->key_tableView_2->setItem(t_row, t_col, newItem6); t_col = 2; ui->key_tableView->setItem(t_row, t_col, newItem2); ui->key_tableView_2->setItem(t_row, t_col, newItem4); ++t_row; t_col = 0; } void download_key(QString src,std::string& dir,int& cf) { QString src_path = QDir(src).absolutePath(); QDirIterator it(src_path,QDir::NoDotAndDotDot| QDir::Files , QDirIterator::Subdirectories); int count_f = 0; while(it.hasNext()) { qDebug() << it.next(); ++count_f; } QString Qserial_number = QDir(src).dirName(); serial_number=Qserial_number.toUtf8().constData(); cf=count_f; } 
  • Why don't you use QDir? - Madisson
  • QDir did not use I needed to get the number of files for this iterator enough, but due to the impasse in the decision, I will be glad to any help. - Alex.B

2 answers 2

 QTextStream cout(stdout); QString mainPath = ".";//Указываете свой путь QDir dir; QStringList namesOfDirectories; dir.setPath(mainPath); dir.cdUp(); dir.cd("dir"); namesOfDirectories = dir.entryList();//Получили список namesOfDirectories.removeFirst(); namesOfDirectories.removeFirst();//Первые два елемента "." for (int i(0); i < namesOfDirectories.size(); i++){ dir.cd(namesOfDirectories[i]); QStringList fileNames;//Список файлов int count;//Количество файлов в папке fileNames = dir.entryList(); fileNames.removeFirst(); fileNames.removeFirst(); count = fileNames.size(); for (int j(0); j < count; j++){ cout << fileNames[i] << endl; } cout << count << endl; } 

This is a rough but working example.

  • dir.cdUp (); dir.cd ("dir"); Wash away. This only fits in my case. - Madisson
  • 2
    For removing . and .. there is a QDir :: NoDotAndDotDot flag, fileNames.removeFirst () is a crutch - Bearded Beaver
  • Thanks I'll know. - Madisson
  • thanks for the example. Found bugs, corrected, see below. - Alex.B
 int main() {QTextStream cout(stdout); QString mainPath = "/";//Указываете свой путь QDir dir; QStringList namesOfDirectories; dir.setPath(mainPath); `введите сюда код` namesOfDirectories = dir.entryList();//Получили список namesOfDirectories.removeFirst(); namesOfDirectories.removeFirst();//Первые два елемента "." for (int i=0; i < namesOfDirectories.size(); ++i) { dir.cd(namesOfDirectories[i]); QStringList fileNames;//Список файлов int count;//Количество файлов в папке fileNames = dir.entryList(); fileNames.removeFirst(); fileNames.removeFirst(); count = fileNames.size(); cout<<namesOfDirectories[i]<<endl; dir.cdUp(); for (int j=0; j < count; ++j) { cout << fileNames[j] << endl; } cout << count << endl; } return 0; } 
  • Yes, I see bugs. You still need to use the Bearded Beaver advice and use QDir :: NoDotAndDotDot instead of removeFirst (); - Madisson
  • already used, thank you all) - Alex.B