Bug report #7075

Create WFS Layer in stand alone python script

Added by Jeremy Palmer almost 12 years ago. Updated almost 12 years ago.

Status:Closed
Priority:Normal
Assignee:Marco Hugentobler
Category:Web Services clients/WFS
Affected QGIS version:master Regression?:No
Operating System:ubuntu Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:16159

Description

The following script hangs with CPU 100%:

from PyQt4.QtCore import *
from PyQt4.QtXml import *
from qgis.core import *
from qgis.gui import *

QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

sourceUri = "http://foo/bar" 

layer = QgsVectorLayer(sourceUri, "My WFS layer", "WFS")

outputs following Qt warnings:

QEventLoop: Cannot be used without QApplication
QDBusConnection: system D-Bus connection created before QCoreApplication. Application may misbehave.
QEventLoop: Cannot be used without QApplication

Could this be because the WFS driver normally tries to create a GUI process dialog when running within the QGIS app?

History

#1 Updated by Marco Hugentobler almost 12 years ago

I think the problem is that there is no instance of QgsApplication in the script (QEventLoop: Cannot be used without QApplication). Try something like this (not tested, so only a rough indication):

def main( argv ):
theApplication = QgsApplication( argv, False )
QgsApplication.setPrefixPath( '/usr', True )
providerRegistry = QgsProviderRegistry.instance( QgsApplication.pluginPath() )
sourceUri = "http://foo/bar"
layer = QgsVectorLayer(sourceUri, "My WFS layer", "WFS")

#2 Updated by Marco Hugentobler almost 12 years ago

  • Status changed from Open to Feedback

#3 Updated by Jeremy Palmer almost 12 years ago

  • Status changed from Feedback to Closed

Thanks that works even, if a little non-standard. All other providers I've used don't need this.

#4 Updated by Marco Hugentobler almost 12 years ago

All other providers I've used don't need this.

It is because QNetworkAccessManager class needs a Qt event loop.

Also available in: Atom PDF