Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to tell Qt/PySide2 where to look for the qt.conf file on Windows?



  • When creating a QApplication or QCoreApplication Qt looks for the qt.conf file relative to the applicationFilePath. On Windows the applicationFilePath is always the path name of the actual executable (see qAppFileName in qcoreapplication_win.cpp).

    Usually this is fine as qt.conf is in the same folder as python.exe and the prefix and library paths are picked up from there.

    However, when embedding Python into another application this is a problem as the executable is no longer python.exe. Setting QT_PLUGIN_PATH is a partial solution as that gets the library paths set mostly correctly, but this doesn't solve the issue of some of the other paths not being set correctly (eg DataPath).

    Is there a way to tell Qt where to look for the qt.conf file? Or is there a way to set these other paths with some different environment variables or some other way before initializing the app?

    Thanks!



  • I have tried the "-qtconf" argument when constructing the app but that doesn't do the trick:

    from PySide2.QtWidgets import QApplication
    from PySide2.QtCore import QLibraryInfo
    
    # sys.executable is set to python.exe
    qtconf = os.path.join(os.path.dirname(sys.executable), "qt.conf")
    if not os.path.exists(qtconf):
       raise Exception("qt.conf file not found: %s" % qtconf)
    
    # create the app using the -qtconf argument
    app = QApplication(["-qtconf", qtconf])
    
    # these should be take from the qt.conf file
    print("PREFIX = %s" % QLibraryInfo.location(QLibraryInfo.PrefixPath))
    print("DATA = %s" % QLibraryInfo.location(QLibraryInfo.DataPath))
    

    This prints the following:

    PREFIX = C:/Users/filipe/miniconda3/conda-bld/qt_1598494811799/_h_env/Library
    DATA = C:/Users/filipe/miniconda3/conda-bld/qt_1598494811799/_h_env/Library
    

    which looks like the paths from when the Python packages were built.

    Something similar happens with PyQt5 in that it doesn't pick up the qt.conf file from the arguments, but it does at least default the locations to its own copy of the qt libs in its site-packages folder.

    Any suggestions of anything else to try greatly appreciated!



  • Building a resource containing "/qt/etc/qt.config" and using QResource.registerResource before creating the app works. This is ok but is a bit complicated and involves building the resource from Python with rcc. Still hoping for a simpler way!



  • @Tony32423 said in How to tell Qt/PySide2 where to look for the qt.conf file on Windows?:

    Usually this is fine as qt.conf is in the same folder as python.exe

    I'm sorry I'm not providing any solution here, but this sounds awful! Why would a Qt configuration file be placed in the same directory of the general python.exe program?

    Something similar happens with PyQt5 in that it doesn't pick up the qt.conf file from the arguments, but it does at least default the locations to its own copy of the qt libs in its site-packages folder.

    This sounds much better. The qt.conf file should surely be placed somewhere inside Qt stuff.


Log in to reply