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 to show folder with certain file name in it

How to show folder with certain file name in it

Scheduled Pinned Locked Moved General and Desktop
38 Posts 5 Posters 25.6k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lahianim
    wrote on last edited by
    #22

    sorry but i don't understand what you need to do exactly.
    you asked to show the user only the folder that include abc.txt and that what the code do.
    if you want that the user will show only the abc.txt files you can add a QRegExp to the MyFilterProxy and (i think) to remove the filterAcceptsRow

    or try this:
    @
    QFileDialog fileDialog;
    fileDialog.setFilter("abc.txt");
    fileDialog.exec();
    @

    the dialog will show you the file system and when you'll enter a folder with file name exactlly abc.txt you will see it and have the option to select it (the filter will ignore all other files name)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SherifOmran
      wrote on last edited by
      #23

      here is my last code.

      1- your code in the main, breaks on mac but the following works. It seems declaration should be only as pointers
      @
      myclass myProxy = new myclass;
      QFileDialog fileDialog = new QFileDialog;
      //fileDialog->setDirectory(qApp->applicationDirPath());
      //its your call if to use this line (or default or other view mode)
      fileDialog->setViewMode(QFileDialog::Detail);
      fileDialog->setProxyModel(myProxy);
      fileDialog->exec();
      @
      2- In myclass, I use the following code
      @ QModelIndex index = sourceModel()->index(source_row, 0/column/, source_parent);
      QFileSystemModel
      filemodel = qobject_cast<QFileSystemModel
      >(sourceModel());
      // QFileSystemModel filemodel = static_cast<QFileSystemModel>(sourceModel());
      filemodel->setRootPath(QDir::currentPath());
      QString fullPath = filemodel->fileInfo(index).absoluteFilePath();
      QDir dir(fullPath);
      QStringList children = dir.entryList( QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
      qDebug () << children;
      @
      It prints now the files in the folders

      Now I need to check in the Qstringlist for the file "abc.txt" and return true for this file only otherwise false.

       foreach (QString str,children)
          {
              
          }
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #24

        Yes, I changed the target, instead of showing the folder that contains the file. I show the files and make the file "abc.txt" selectable .

        The setfilter does not work properly, it shows me all files are unselectable
        @QFileDialog fileDialog;
        fileDialog.setFilter("abc.txt");
        fileDialog.exec();
        @

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lahianim
          wrote on last edited by
          #25

          first about your code bq. foreach (QString str,children) { }
          you can use:
          @if(children.contains("abc.txt")) return true; else false;@

          and about your last message:
          [quote author="SherifOmran" date="1344846279"]Yes, I changed the target, instead of showing the folder that contains the file. I show the files and make the file "abc.txt" selectable .

          [/quote]
          lets say you have 2 files named "abc.txt" at the paths:
          "C:\some_folder\abc.txt" and
          "D:\other_folder\new_folder\abc.txt"

          what you want the user will show in the QFileDialog?
          how the user will navigate to the paths if he will not see the folders (of the file system)

          please explain in more details as you can, what you are asking for and what you want the user will see, and if you have a image or you see your request working some where please refer to it

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SherifOmran
            wrote on last edited by
            #26

            I want the user to see all folders but when he gets inside it, he does not see except only the abc.txt file to select it, all other files are hidden. something like in any open file in a windows program, say winword in the open window one sees *.doc but I can write abc.doc in the window and it will be filtered automatically.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lahianim
              wrote on last edited by
              #27

              hi i used this code: (as before)
              @
              #include <QtGui/QApplication>
              #include <QFileDialog>

              int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);
              QFileDialog fileDialog;
              fileDialog.setViewMode(QFileDialog::Detail);
              fileDialog.setFilter("csb.log");
              fileDialog.exec();
              return a.exec();
              }
              @

              and is work it show me all folder and when i insert to folder if there are files he show me only the
              csb.log if it exist or only folder if not
              i don't have a MAC to test it so i don't know why it's not working to you

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SherifOmran
                wrote on last edited by
                #28

                This code works on MAC only with a *

                @
                fileDialog.setFilter("*.log");
                @

                but once you put a filename, all files are disabled.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SherifOmran
                  wrote on last edited by
                  #29

                  see this, setfilter controls only the file type
                  @
                  void QFileDialog::setFilter ( QDir::Filters filters )
                  Sets the filter used by the model to filters. The filter is used to specify the kind of files that should be shown.

                  This function was introduced in Qt 4.4.
                  @

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SherifOmran
                    wrote on last edited by
                    #30

                    we are 1 step forward, i use this in the mycalss.cpp
                    @
                    QStringList files = dir.entryList(QStringList("abc.txt"),
                    QDir::Files | QDir::NoSymLinks);

                        qDebug () << files << files.contains("abc.txt");
                    

                    @
                    now I get in the debug when I click on browse

                    @
                    () false
                    ("abc.txt") true
                    () false
                    @

                    but it ignores my if condition simply and shows me all the files ...

                    any idea? may be i ve a mistake in the call?

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lahianim
                      wrote on last edited by
                      #31

                      [quote author="SherifOmran" date="1344933412"]we are 1 step forward, i use this in the mycalss.cpp

                      but it ignores my if condition simply and shows me all the files ...
                      [/quote]
                      which if condition?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SherifOmran
                        wrote on last edited by
                        #32

                        class

                        @

                        bool myclass::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
                        {

                        QModelIndex index = sourceModel()->index(source_row, 0/*column*/, source_parent);
                             QFileSystemModel* filemodel = qobject_cast<QFileSystemModel*>(sourceModel());
                            // QFileSystemModel *filemodel = static_cast<QFileSystemModel*>(sourceModel());
                            filemodel->setRootPath(QDir::currentPath());
                            QString fullPath =   filemodel->fileInfo(index).absoluteFilePath();
                            QDir dir(fullPath);
                        
                            QStringList  files = dir.entryList(QStringList("abc.txt"),
                                                         QDir::Files | QDir::NoSymLinks);
                        
                            qDebug () << files << files.contains("abc.txt");
                        

                        if (files.contains("abc.txt")) return true; else false;

                        }

                        @

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lahianim
                          wrote on last edited by
                          #33

                          so you need to change the filterAcceptsRow function to be as:
                          if the index represent a folder return true
                          else if its a file and his name == to "abc.txt" return true
                          else return false
                          in that case you will get all folder (as empty) and you'll see only the file with name "abc.txt"

                          @
                          bool myclass::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
                          {
                          QModelIndex index = sourceModel()->index(source_row, 0/column/, source_parent);
                          QFileSystemModel* filemodel = qobject_cast<QFileSystemModel*>(sourceModel());
                          QFileInfo indexInfo = filemodel->fileInfo(index);
                          if( indexInfo.isDir())
                          return true;
                          else if (indexInfo.isFile() && indexInfo.fileName() == QString("abc.txt"))
                          return true;
                          else return false;
                          }
                          @

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SherifOmran
                            wrote on last edited by
                            #34

                            It does not work.

                            I use this as the calling function, is it correct?
                            @
                            QFileDialog *fileDialog = new QFileDialog;
                            myclass *sourceModel = new myclass;
                            QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
                            proxyModel->setSourceModel(sourceModel);
                            fileDialog->setProxyModel(proxyModel);
                            fileDialog->show();
                            @

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SherifOmran
                              wrote on last edited by
                              #35

                              please see this link, it seems there is a bug
                              https://bugreports.qt-project.org/browse/QTBUG-7739

                              1 Reply Last reply
                              0
                              • L Offline
                                L Offline
                                lahianim
                                wrote on last edited by
                                #36

                                the bug report refer to code like this:
                                @
                                QFileDialog fileDialog;
                                fileDialog.setFilter("abc.txt");
                                fileDialog.exec();
                                @

                                and not for the code using QSortFilterProxyModel.
                                about your code:
                                [quote author="SherifOmran" date="1344951506"]It does not work.

                                I use this as the calling function, is it correct?
                                @
                                QFileDialog *fileDialog = new QFileDialog;
                                myclass *sourceModel = new myclass;
                                QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
                                proxyModel->setSourceModel(sourceModel);
                                fileDialog->setProxyModel(proxyModel);
                                fileDialog->show();
                                @
                                .
                                [/quote]

                                you use it in the wrong way instead use it as follow

                                @
                                QFileDialog *fileDialog = new QFileDialog;
                                //you implemented MyClass as proxy model so you need to set it in QFileDialog as proxyModel
                                myclass *proxyModel = new myclass;
                                fileDialog->setProxyModel(proxyModel);
                                fileDialog->show();
                                @

                                and use the filterAcceptRow as before

                                [quote author="lahianim" date="1344944559"]so you need to change the filterAcceptsRow function to be as:
                                if the index represent a folder return true
                                else if its a file and his name == to "abc.txt" return true
                                else return false
                                in that case you will get all folder (as empty) and you'll see only the file with name "abc.txt"

                                @
                                bool myclass::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
                                {
                                QModelIndex index = sourceModel()->index(source_row, 0/column/, source_parent);
                                QFileSystemModel* filemodel = qobject_cast<QFileSystemModel*>(sourceModel());
                                QFileInfo indexInfo = filemodel->fileInfo(index);
                                if( indexInfo.isDir())
                                return true;
                                else if (indexInfo.isFile() && indexInfo.fileName() == QString("abc.txt"))
                                return true;
                                else return false;
                                }
                                @[/quote]

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SherifOmran
                                  wrote on last edited by
                                  #37

                                  even this, it does not work my friend on MAC. It seems to be a bug.

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SherifOmran
                                    wrote on last edited by
                                    #38

                                    I made a bug report

                                    https://bugreports.qt-project.org/browse/QTBUG-26856?page=com.atlassian.jirafisheyeplugin:fisheye-issuepanel

                                    1 Reply Last reply
                                    0

                                    • Login

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