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. QFileDialog: block access to folders or drives on Windows
Qt 6.11 is out! See what's new in the release blog

QFileDialog: block access to folders or drives on Windows

Scheduled Pinned Locked Moved Solved General and Desktop
qfiledialogwindows
13 Posts 3 Posters 7.9k Views 2 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.
  • kshegunovK kshegunov

    @the_
    Hello,
    Aren't the flags working for you, namely QFileDialog::FileMode and QFileDialog::Option?

    Kind regards.

    the_T Offline
    the_T Offline
    the_
    wrote on last edited by
    #3

    @kshegunov

    I am not sure if that will work for me, FileMode only sets the selection mode, as I need to select single files it is set to QFileDialog::ExistingFile. As i correctly understand the QFileDialog::Option there is no way to deny access to some path

    Maybe I should add some more description to my problem:

    The app i wrote is a reporting tool for special forms. Users are able to add attachments to each report but should not be able to access every drive, for example, if you have C: (the OS drive itself), D: (a flash drive, usb stick, whatever) and X: (a network drive), any access to C: should be denied (by doubleclicking this drive in "Computer", by entering C:<any path><anyfile> in the address bar and by selecting the drive and click the Open button in the dialog)

    -- No support in PM --

    kshegunovK 1 Reply Last reply
    0
    • the_T the_

      @kshegunov

      I am not sure if that will work for me, FileMode only sets the selection mode, as I need to select single files it is set to QFileDialog::ExistingFile. As i correctly understand the QFileDialog::Option there is no way to deny access to some path

      Maybe I should add some more description to my problem:

      The app i wrote is a reporting tool for special forms. Users are able to add attachments to each report but should not be able to access every drive, for example, if you have C: (the OS drive itself), D: (a flash drive, usb stick, whatever) and X: (a network drive), any access to C: should be denied (by doubleclicking this drive in "Computer", by entering C:<any path><anyfile> in the address bar and by selecting the drive and click the Open button in the dialog)

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #4

      @the_
      I see, what about setting a proxy model to the dialog and filtering up the restricted locations there? I think it should work in your case.

      Read and abide by the Qt Code of Conduct

      the_T 3 Replies Last reply
      1
      • kshegunovK kshegunov

        @the_
        I see, what about setting a proxy model to the dialog and filtering up the restricted locations there? I think it should work in your case.

        the_T Offline
        the_T Offline
        the_
        wrote on last edited by
        #5

        @kshegunov

        Sounds to be a proper way. Thanks for the hint. I will give it a try and post an update.

        -- No support in PM --

        1 Reply Last reply
        0
        • kshegunovK kshegunov

          @the_
          I see, what about setting a proxy model to the dialog and filtering up the restricted locations there? I think it should work in your case.

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #6

          @kshegunov
          Well I had some "funny" things here about ProxyModels....

          I found an example on stackoverflow, which i tried to implement:

          class FileFilterProxyModel : public QSortFilterProxyModel
          {
          protected:
              virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
          };
          
          bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
          {
              QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
              QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
          
              if (fileModel!=NULL && fileModel->isDir(index0))
              {
                  if(fileModel->filePath(index0).startsWith("/home/")) {
                      qDebug() << fileModel->filePath(index0);
                      //        qDebug() << fileModel->fileName(index0);
                      return true;
                  }
                  else
                      return false;
              }
              else
                  return false;
              // uncomment to execute default implementation
              //return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
          }
          
          //...
          filed = new QFileDialog(this);
          FileFilterProxyModel *ffpm = new FileFilterProxyModel;
          filed->setProxyModel(ffpm);
          qDebug() << filed->proxyModel();
          

          QFileDialog *filed; is defined in the header file;

          When I run this on my linux box it returns

          QSortFilterProxyModel(0x7c3a10) QSortFilterProxyModel(0x7c3a10)
          

          but when I run the same code (ok not exactly the same, I changed the "/home/" to "C:" :) ) it returns

          QSortFilterProxyModel(0x7c3a10) QObject(0x0)
          

          The only difference is that Win8.1 has Qt5.4.2 and my Arch has Qt5.5.1...

          Is there something wrong?

          -- No support in PM --

          kshegunovK 1 Reply Last reply
          0
          • the_T the_

            @kshegunov
            Well I had some "funny" things here about ProxyModels....

            I found an example on stackoverflow, which i tried to implement:

            class FileFilterProxyModel : public QSortFilterProxyModel
            {
            protected:
                virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
            };
            
            bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
            {
                QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
                QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
            
                if (fileModel!=NULL && fileModel->isDir(index0))
                {
                    if(fileModel->filePath(index0).startsWith("/home/")) {
                        qDebug() << fileModel->filePath(index0);
                        //        qDebug() << fileModel->fileName(index0);
                        return true;
                    }
                    else
                        return false;
                }
                else
                    return false;
                // uncomment to execute default implementation
                //return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
            }
            
            //...
            filed = new QFileDialog(this);
            FileFilterProxyModel *ffpm = new FileFilterProxyModel;
            filed->setProxyModel(ffpm);
            qDebug() << filed->proxyModel();
            

            QFileDialog *filed; is defined in the header file;

            When I run this on my linux box it returns

            QSortFilterProxyModel(0x7c3a10) QSortFilterProxyModel(0x7c3a10)
            

            but when I run the same code (ok not exactly the same, I changed the "/home/" to "C:" :) ) it returns

            QSortFilterProxyModel(0x7c3a10) QObject(0x0)
            

            The only difference is that Win8.1 has Qt5.4.2 and my Arch has Qt5.5.1...

            Is there something wrong?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #7

            @the_
            Hello,
            If there were, I can't spot it directly. There's one thing I don't understand though, where are those addresses printed and what they're supposed to mean.

            When I run this on my linux box it returns
            QSortFilterProxyModel(0x7c3a10) QSortFilterProxyModel(0x7c3a10)
            but when I run the same code (ok not exactly the same, I changed the "/home/" to "C:" :) ) it returns
            QSortFilterProxyModel(0x7c3a10) QObject(0x0)

            This I don't get. What do you mean by "it returns" and what's the relation to: "QSortFilterProxyModel(0x7c3a10)"?

            Kind regards.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • kshegunovK kshegunov

              @the_
              I see, what about setting a proxy model to the dialog and filtering up the restricted locations there? I think it should work in your case.

              the_T Offline
              the_T Offline
              the_
              wrote on last edited by
              #8

              @kshegunov

              That's what qDebug prints. Sorry for being not precise enough.

              -- No support in PM --

              kshegunovK 1 Reply Last reply
              0
              • the_T the_

                @kshegunov

                That's what qDebug prints. Sorry for being not precise enough.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #9

                @the_
                Not about precision, I just don't understand the meaning of it.
                Is this:

                qDebug() << filed->proxyModel();
                

                printing those?

                Additionally, where does this piece of code:

                filed = new QFileDialog(this);
                FileFilterProxyModel *ffpm = new FileFilterProxyModel;
                filed->setProxyModel(ffpm);
                qDebug() << filed->proxyModel();
                

                reside?

                Read and abide by the Qt Code of Conduct

                the_T 1 Reply Last reply
                0
                • kshegunovK kshegunov

                  @the_
                  Not about precision, I just don't understand the meaning of it.
                  Is this:

                  qDebug() << filed->proxyModel();
                  

                  printing those?

                  Additionally, where does this piece of code:

                  filed = new QFileDialog(this);
                  FileFilterProxyModel *ffpm = new FileFilterProxyModel;
                  filed->setProxyModel(ffpm);
                  qDebug() << filed->proxyModel();
                  

                  reside?

                  the_T Offline
                  the_T Offline
                  the_
                  wrote on last edited by
                  #10

                  @kshegunov

                  The thing is, that I did not get the expected result on windows, so i checked if the proxymodel is applied correctly, which seems to be not.

                  The code is inside the constructor of the class that builds the ui where i want to use the QFileDialog.

                  mainwindow.cpp

                  //includes
                  
                  class FileFilterProxyModel : public QSortFilterProxyModel {
                   //class goes here 
                  };
                  
                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow) {
                  
                  //...
                  
                  filed = new QFileDialog(this);
                  FileFilterProxyModel *ffpm = new FileFilterProxyModel;
                  filed->setProxyModel(ffpm);
                  qDebug() << filed->proxyModel();
                  
                  //...
                  }
                  

                  Thats for short the structure.
                  The part I do not understand is why on linux proxyModel() returns the one I set and on windows it returns a NULL Object.

                  -- No support in PM --

                  kshegunovK 1 Reply Last reply
                  0
                  • the_T the_

                    @kshegunov

                    The thing is, that I did not get the expected result on windows, so i checked if the proxymodel is applied correctly, which seems to be not.

                    The code is inside the constructor of the class that builds the ui where i want to use the QFileDialog.

                    mainwindow.cpp

                    //includes
                    
                    class FileFilterProxyModel : public QSortFilterProxyModel {
                     //class goes here 
                    };
                    
                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow) {
                    
                    //...
                    
                    filed = new QFileDialog(this);
                    FileFilterProxyModel *ffpm = new FileFilterProxyModel;
                    filed->setProxyModel(ffpm);
                    qDebug() << filed->proxyModel();
                    
                    //...
                    }
                    

                    Thats for short the structure.
                    The part I do not understand is why on linux proxyModel() returns the one I set and on windows it returns a NULL Object.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #11

                    @the_
                    Hello,

                    The thing is, that I did not get the expected result on windows, so i checked if the proxymodel is applied correctly, which seems to be not.

                    It should, but you may have stumbled upon a bug, however before jumping the gun let's suppose there's a subtle problem with the code (still) and try to fix that. About your qDebug() statement, the thing that baffled me is that you output a single address with this:

                    qDebug() << filed->proxyModel();
                    

                    but you get two objects output:

                    QSortFilterProxyModel(0x7c3a10) QSortFilterProxyModel(0x7c3a10)
                    

                    Am I misunderstanding?

                    Your constructor looks okay, as far as I can tell, but you should (at least I believe you should) also disable the nativeness of the dialog, provided you haven't done already, like this:

                    filed->setOptions(QFileDialog::DontUseNativeDialog);
                    

                    Let me know how that turns out.

                    Kind regards.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1
                    • the_T the_

                      Hi guys,

                      Is there a proper way to block access to folders and/or drives ?
                      There is a signal, that is emitted when changing a directory, but this seems to work only if you doubleclick a folder, so i can check if the returned directory path is valid or not. But how to disable access via the address bar or the "Open" button?

                      Thanks for help

                      D Offline
                      D Offline
                      Devopia53
                      wrote on last edited by
                      #12

                      @the_

                      As said @kshegunov,

                      ProxyModel is unsupported feature on native dialog mode.

                      the_T 1 Reply Last reply
                      1
                      • D Devopia53

                        @the_

                        As said @kshegunov,

                        ProxyModel is unsupported feature on native dialog mode.

                        the_T Offline
                        the_T Offline
                        the_
                        wrote on last edited by
                        #13

                        @Devopia53

                        This did it with Windows. Thanks

                        -- No support in PM --

                        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