7

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.)

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Oct 20, 2021 at 18:57
5
  • 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. Commented Oct 21, 2021 at 15:52
  • 4
    I don't think you can do it indeed, you need to replicate the file indeed main.cpp ... Commented Oct 21, 2021 at 17:17
  • I opened a related ticket on QGIS GitHub: github.com/qgis/QGIS/issues/45612 Commented Oct 22, 2021 at 9:04
  • 1
    @gmas80 Out of curiosity, in what situation would it be useful? Commented Nov 1, 2021 at 20:09
  • @ComradeChe, for instance, to create a frozen version of a customized QGIS using PyInstaller. Commented Nov 2, 2021 at 13:56

1 Answer 1

11

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()

enter image description here

answered Oct 20, 2021 at 20:24
1
  • 1
    Thank 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. Commented Oct 21, 2021 at 15:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.