2

In PyCharm I have a python environment setup with a Bat file so that I can use QGIS and OSGeo python environment in my scripts. I started looking into ui's and using this code

from PyQt5 import QtWidgets, uic
import sys

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('basic.ui', self)
        self.show()

app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

I get a window pop up error

"This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem"

Is there some way to use the QtCreator installed with OSGeo for QGIS by changing my imports or environment settings or do I need to install QtCreator Community to get this to work?

BAT File

    @echo off
    SET OSGEO4W_ROOT=C:\OSGeo4W64
    call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
    call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
    @echo off
    path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
    path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass78\lib
    path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
    path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts

    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
    set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37

    set PATH=C:\Program Files\Git\bin;%PATH%

start "PyCharm aware of QGIS" /B "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2021.1.1\bin\pycharm64.exe"
4
  • Please add the batch file you are using to set the pyqgis/ pyqt environment and version of QGIS in use.
    – Ben W
    Nov 5, 2022 at 0:53
  • @BenW I added the bat file code to the question.
    – MrKingsley
    Nov 7, 2022 at 12:57
  • Try adding following line to batch file: set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
    – Ben W
    Nov 7, 2022 at 13:06
  • @BenW, I added set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins just under the line set PATH=C:\Program Files\Git\bin;%PATH% and the error still came up. Did I put it in the wrong spot?
    – MrKingsley
    Nov 10, 2022 at 15:13

1 Answer 1

0

Using BenW's input and this source https://haifengniu.com/post/how-to-set-environment-for-pyqgis-3-in-pycharm/ I was able to determine my bat file that sets my environments in pyCharm was missing some statements that were required in order to get the full OSGeo4w environment working in pyCharm. With the bat file below I was able to get past the application error.

    @echo off
    SET OSGEO4W_ROOT=C:\OSGeo4W64
    call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
    call "%OSGEO4W_ROOT%"\bin\qt5_env.bat ##Added to resolve error##
    call "%OSGEO4W_ROOT%"\bin\py3_env.bat ##Added to resolve error##
    call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
    @echo off
    path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
    path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass78\lib
    path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
    path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts

    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
    set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
    set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\Qt5\plugins ##Added to resolve error##
    set PATH=C:\Program Files\Git\bin;%PATH%

start "PyCharm aware of QGIS" /B "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2021.1.1\bin\pycharm64.exe"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.