I looked at the QGIS official documentation (e.g., Using PyQGIS in custom applications) and a few questions here (e.g. Running QGIS through Python), but I was unable to find an example of a Python script that starts a regular QGIS app.
In other words, I look for the 'official'/recommended way to replicate the same results of running QGIS.exe
(splash screen, main window visualization, etc.) using a qgis.py
instead (i.e., without calling a QGIS executable using subprocess):
import logging
from qgis import core
logger = logging.getLogger(__name__)
def main():
qgis_version = core.Qgis.version()
logger.info('Running QGIS v.%s ...' % qgis_version)
qg = core.QgsApplication([], True)
qg.initQgis()
# some code here that runs the QGIS splashscreen?
# some code here that shows the QGIS main window?
qg.exec_()
qg.exitQgis()
logger.info('Running QGIS v.%s ... DONE' % qgis_version)
(I am aware that it is possible to execute a startup.py, but this question is about writing a Python script that starts QGIS up.)
-
For reference, this is how a regular QGIS app is started. github.com/qgis/QGIS/blob/master/src/app/main.cpp It's not a trivial task to replicate.Germán Carrillo– Germán Carrillo2021年10月21日 15:52:40 +00:00Commented Oct 21, 2021 at 15:52
-
4I don't think you can do it indeed, you need to replicate the file indeed main.cpp ...etrimaille– etrimaille2021年10月21日 17:17:19 +00:00Commented Oct 21, 2021 at 17:17
-
I opened a related ticket on QGIS GitHub: github.com/qgis/QGIS/issues/45612gmas80– gmas802021年10月22日 09:04:03 +00:00Commented Oct 22, 2021 at 9:04
-
1@gmas80 Out of curiosity, in what situation would it be useful?Comrade Che– Comrade Che2021年11月01日 20:09:10 +00:00Commented Nov 1, 2021 at 20:09
-
@ComradeChe, for instance, to create a frozen version of a customized QGIS using PyInstaller.gmas80– gmas802021年11月02日 13:56:38 +00:00Commented Nov 2, 2021 at 13:56
1 Answer 1
You can use the pattern in the script:
from qgis import core
import subprocess
def main():
qg = core.QgsApplication([], True)
qg.initQgis()
print("Starting QGIS")
qgis_exe = r"C:\OSGeo4W64\bin\qgis-bin.exe"
process = subprocess.Popen(qgis_exe, shell=True, stdout=subprocess.PIPE)
process.wait()
qg.exitQgis()
print("QGIS is closed now")
main()
-
1Thank you for the proposed solution. I am looking for a solution that does not call a .exe, but just replicating the results of calling qgis-bin.exe. I have modified my question. Hope that it is clearer now.gmas80– gmas802021年10月21日 15:31:21 +00:00Commented Oct 21, 2021 at 15:31