Search code examples
pythonwindowsqtazureqfiledialog

Preventing file actions in QFileDialog, such as copying, viewing, deleting, etc


We are looking at deploying a PyQt application on an Azure server, and the application works well enough, albeit a little slow to respond to user actions.

We have a problem, however, and that is that the QFileDialog allows pretty much any explore action: copy a file from the virtual machine to the user's local drive, open a file within 'Program Files (x86)' in Notepad, etc.

Approaches already considered:

  1. As the python application has to have read and write permissions to run under 'Program Files (x86)', we can't use file permissions to control access.

  2. We can turn the Python into an inscrutable .exe, but this could still be copied using the context menus in the file dialog.

  3. We could use the file filters and then hide them, so you can only see (and mess with) the relevant files, but the user could still copy entire directories.

The only thing we can think of is to create our own file dialog from scratch, but that's very tedious. Are there any 'out of the box' solutions?


Solution

  • The QFileDialog class already has this functionality:

        dialog = QtGui.QFileDialog()
        dialog.setOption(QtGui.QFileDialog.ReadOnly, True)
        dialog.exec_()
    

    This only seems to work with Qt's built-in file-dialog, though. If you use the static functions to open a native file-dialog, the ReadOnly option seems to be ignored (I've only tested this on Linux, though).