Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i use setFilterName() public function by taking input from user?
Qt 6.11 is out! See what's new in the release blog

How can i use setFilterName() public function by taking input from user?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 731 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • eefesafakE Offline
    eefesafakE Offline
    eefesafak
    wrote on last edited by
    #1

    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.
    Screenshot from 2022-05-09 11-25-40.png
    Thanks for your precious time. Best regards!

    jsulmJ JonBJ 2 Replies Last reply
    0
    • eefesafakE eefesafak

      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.
      Screenshot from 2022-05-09 11-25-40.png
      Thanks for your precious time. Best regards!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • eefesafakE eefesafak

        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.
        Screenshot from 2022-05-09 11-25-40.png
        Thanks for your precious time. Best regards!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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 a QLineEdit where the user can type this in, and then you pick it up and transfer it to your QFileDialog or QDir.entryInfoList() code.

        eefesafakE JonBJ 2 Replies Last reply
        2
        • JonBJ JonB

          @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 a QLineEdit where the user can type this in, and then you pick it up and transfer it to your QFileDialog or QDir.entryInfoList() code.

          eefesafakE Offline
          eefesafakE Offline
          eefesafak
          wrote on last edited by
          #4

          @JonB If user enters input separated by semi colon into QLineEdit, that's my goal.

          1 Reply Last reply
          0
          • JonBJ JonB

            @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 a QLineEdit where the user can type this in, and then you pick it up and transfer it to your QFileDialog or QDir.entryInfoList() code.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @eefesafak

            @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 your QFileDialog or QDir.entryInfoList() code.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved