PyQt5 set qt_ntfs_permission_lookup

194 views Asked by At

I want to use isWritable() from QFileInfo. According to the docs, you have to somehow set qt_ntfs_permission_lookup to 1 to get a meaningful result on Windows. The C++ code for this is

extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
qt_ntfs_permission_lookup++; // turn checking on
qt_ntfs_permission_lookup--; // turn it off again

How do I "translate" the extern statement into Python?

1

There are 1 answers

0
eyllanesc On BEST ANSWER

One possible solution is to create functions that change the state of that variable in C++ and export it to python. To export a C++ function to python there are options like pybind11, SWIG, sip, shiboken2, etc.

In this case, implement a small library using pybind11

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

#ifdef Q_OS_WIN
QT_BEGIN_NAMESPACE
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
QT_END_NAMESPACE
#endif

PYBIND11_MODULE(qt_ntfs_permission, m) {

    m.def("enable", [](){
#ifdef Q_OS_WIN
        qt_ntfs_permission_lookup = 1;
#endif
    });      
    m.def("disable", [](){
#ifdef Q_OS_WIN
        qt_ntfs_permission_lookup = 0;
#endif
    });      

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

and you can install it by following these steps:

Requirements:

  • Qt5
  • Visual Studio
  • cmake
git clone https://github.com/eyllanesc/qt_ntfs_permission_lookup.git
python setup.py install

Also with the help of github actions I have created the wheels for some versions of Qt and python so download it from here, extract the .whl and run:

python -m pip install qt_ntfs_permission-0.1.0-cp38-cp38-win_amd64.whl

Then you run it as:

from PyQt5.QtCore import QFileInfo

import qt_ntfs_permission

qt_ntfs_permission.enable()
qt_ntfs_permission.disable()