How can i use setFilterName() public function by taking input from user?
-
My goal is to list files with specified extension on my program. For now, I can only list .h and .cpp files. But, If an user wants to show specified file (ie. *.txt and *.c at the same time), user is not able to do this. How can this problem be solved?
My code is here:#include "mainwindow.h" #include "qheaderview.h" #include "qtablewidget.h" #include "ui_mainwindow.h" #include "countline.h" // kullanıcı seçmeli // ui_ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed); ui->tableWidget->setColumnWidth(0, 482); ui->tableWidget->setColumnWidth(1, 150); ui->tableWidget->setColumnWidth(2, 150); } MainWindow::~MainWindow() { delete ui; } void MainWindow::addItemToList(const QString &file, const int &fileSize, const int &count) { QTableWidgetItem *iFileName = new QTableWidgetItem(file); QTableWidgetItem *iFileSize = new QTableWidgetItem(QString::number(fileSize)); QTableWidgetItem *iFileCount = new QTableWidgetItem(QString::number(count)); int rowCount = ui->tableWidget->rowCount(); ui->tableWidget->insertRow(rowCount); rowCount = ui->tableWidget->rowCount(); ui->tableWidget->setItem(rowCount-1, 0, iFileName); ui->tableWidget->setItem(rowCount-1, 1, iFileSize); ui->tableWidget->setItem(rowCount-1, 2, iFileCount); } QFileInfoList MainWindow::getFileListFromDir(const QString &directory) { QDir qdir(directory); QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h" << "*.hpp" << "*.c" << "*.cpp", QDir::Files, QDir::Size); for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } return fileList; } void MainWindow::on_Browse_clicked() { QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if(path.isEmpty()) return; ui->FullPath->setText(path); } void MainWindow::on_Ok_clicked() { QString path = ui->FullPath->text(); if(path.isEmpty()) return; ui->tableWidget->setRowCount(0); QFileInfoList fileList = getFileListFromDir(path); int count = 0; int sum = 0; foreach(const QFileInfo& file, fileList) { count = m_ig->funcCountLines(file.filePath()); sum += count; addItemToList(file.filePath(), file.size(), count); } ui->label->setText(QString::number(sum)); }
In the documentation, I realise that I can use QFileDialog::setNameFilter(const QString& filters). However, I can not understand how can I get this single or multiple extensions from "Extension" Label or from the program.
Thanks for your precious time. Best regards! -
@eefesafak You can use https://doc.qt.io/qt-5/qfiledialog.html#setNameFilters
You also did not explain what exactly your "Extension" label contains. So, what exactly is there? -
@eefesafak said in How can i use setFilterName() public function by taking input from user?:
But, If an user wants to show specified file (ie. *.txt and *.c at the same time), user is not able to do this. How can this problem be solved?
In the documentation, I realise that I can use QFileDialog::setNameFilter(const QString& filters). However, I can not understand how can I get this single or multiple extensions from "Extension" Label or from the program.
Where do you expect the user to enter the "(ie.
*.txt
and*.c
at the same time)"? To allow arbitrary input like this, you will have to (presumably) have aQLineEdit
where the user can type this in, and then you pick it up and transfer it to yourQFileDialog
orQDir.entryInfoList()
code. -
@JonB said in How can i use setFilterName() public function by taking input from user?:
To allow arbitrary input like this, you will have to (presumably) have a
QLineEdit
where the user can type this in, and then you pick it up and transfer it to yourQFileDialog
orQDir.entryInfoList()
code.