0

I have a code which works in python console of QGIS. I am trying to work in an IDE but it does not work. I have changed it in different ways but it still returns:

QPixmap: Must construct a QApplication before a QPaintDevice

my code is:

import processing
from qgis.core import*
from PyQt4.QtCore import *
import math
from collections import defaultdict
from shapely.geometry import LineString
import json
import datetime
import time
import itertools
import glob
import sys
from nltk import metrics

Class matching():
    def __init__(self):

        self.layers = []

        self.Dir_OSM_roads="D:/PhD works/PhD things/Julian/Matching_data/InputData/SHP/OSM_roads_no_psv_latin1_id.shp"    

    def laodLayer(self):
        self.layers[0]=QgsVectorLayer(self.Dir_OSM_roads,"test" , "ogr")
        QgsMapLayerRegistry.instance().addMapLayer(self.layers[0]);
        print self.layers[0]

    def printValidity(self):
        if not self.layers[0].isValid():
            print "layer does not exist"
        else:
            print "successful"

def main():
    app = QApplication([],True)

    qgis_prefix = "C:/Program Files (x86)/QGIS Valmiera/apps/qgis"

    QgsApplication.setPrefixPath(qgis_prefix, True)
    QgsApplication.initQgis()

    check=matching()
    check.laodLayer()
    check.printValidity()

    retval = app.exec_()

    QgsApplication.exitQgis()
    sys.exit(retval)

if __name__ == '__main__':
    main()

I am working in windows 7, 64 with aptana and QGIS 2.2.

Edit: My windows is 64-bite

msc87
  • 943
  • 3
  • 17
  • 39
  • 1
    Keyword "class" doesn't have first letter "c" capitalized. Also I see that "matching" class's methods have wrong indent. Is it just here on stackoverflow or is it a bug? What IDE are you using? – fox May 22 '14 at 08:36
  • Did a bit of search. I think you better ask such questions at http://gis.stackexchange.com/. And it seems to me that your problem is connected with IDE preferences, here's an example how to setup Aptana Studio for QGIS http://gis.stackexchange.com/questions/62303/how-to-add-qgis-python-api-intellisense-support-in-aptana-studio3-pydev/62334#62334 – fox May 22 '14 at 08:46
  • 2
    The problem here may just be one of formatting. Spacing in python *is very important*. Someone fixed the spacing in your question which may fix the code too. – Gustav Bertram May 22 '14 at 08:57
  • Sorry, but the indention problem was caused by the copy and past. the class was also a typo error here. otherwise everything seems ok in my code and my IDE (eclipse) does not complain about indents and class. I have also checked all possible solutions given in other questions which made me put Qapplication in my code. although it returns the same error yet. – msc87 May 22 '14 at 11:15
  • I had not added qgis-apis to my predefined completions. now I did, but still the same error. – msc87 May 22 '14 at 11:29

0 Answers0