2

I can run a standalone app but no map shows up, but different from a similar question last year. The code posted last year works for me after editing QGISHOME to Win 10 system (r'c:\OSGeo4W64\apps\qgis') and path to my data. Similarly I adapted the code for standalone programs from the latest PyQGIS Programmer's Guide and my data shows up fine. Neither of these examples deal with bringing in a main window from Qt5, leaving me to believe there is a disconnect between Qt5 and PyQGIS that I am unable to bridge.

I built a main window using Qt Designer (Qt5) by following the (Qt4) instructions in geospatialdesktop.com/2009/02/creating_a_standalone_gis_application_1/ tutorial, then added some drop down menus I will attach functions to later.

I edited the tutorial's example code ShapeViewer.py (Qt4, Python 2.x, PyQGIS 2 API) to point to my data and QGIS install. I had to update code to PyQt5, Python 3.x, PyQGIS3 expectations.

For a couple of days my application would load with all menus dropping down fine, but no content in canvas area. Then it failed on an unusual error message: "TypeError: setupGui() takes 1 positional argument but 2 were given." The code (after imports) that creates this error is:

gpkgpath = r'c:\tmp\DVD_surveying\NM\geopackages\nm_survey.gpkg'
class geoGcdb(QMainWindow, Ui_MainWindow):                                              

  def __init__(self):
    QMainWindow.__init__(self)

    self.setupGui(self)
    self.project = QgsProject()

    # Set the title for the app
    self.setWindowTitle("Survey Measurement Exploration and Integration Tools")

    self.add_ogr_layer(gpkgpath+'|layername=filedataset')

  def setupGui(self ):                                                                  
    frame = QFrame(self)                                                                
    self.setCentralWidget(frame)
    #self.grid_layout = QGridLayout(frame)
    self.box_layout = QVBoxLayout(frame)

    self.map_canvas = QgsMapCanvas()
    #self.grid_layout.addWidget(self.map_canvas)
    self.box_layout.addWidget(self.map_canvas)

  def add_ogr_layer(self, path):
    fdslayer = QgsVectorLayer(gpkgpath+'|layername=filedataset', 'file data set', 'ogr')
    if not fdslayer.isValid() :
      print('fdslayer NOT valid!!!')
      exit()
    self.project.addMapLayer(fdslayer)
    self.map_canvas.setExtent(fdslayer.extent())
    self.map_canvas.setLayers([fdslayer])
    self.map_canvas.show()

def main(argv):
  # create Qt application
  app = QApplication(argv)

  # Initialize qgis libraries
  QgsApplication.setPrefixPath(qgis_prefix, True)
  QgsApplication.initQgis()

  # create main window
  wnd = geoGcdb()
  # Move the app window to upper left
  wnd.move(100,100)
  wnd.show()
  # run!
  retval = app.exec_()

  # exit
  wnd = None
  QgsApplication.exitQgis()
  exit(retval)


if __name__ == "__main__":
  main(sys.argv)

In desperation I changed "setupGUI(self)" with "setupGUI(self, path)" and an interesting thing happened. The window appeared with a populated map canvas but no menu items. I am thrashing here trying to juggle two paradigms and obviously not that fluent in object behavior. It would help greatly if I had an updated version of ShapeViewer.py and/or an explanation of why my code behaves unkindly.

2

I solved the problem for this question. The source of my problem seemed to be that Qt5 created a python file containing "def setupUi(self, MainWindow):" and my application code, see above, which called this Qt5-generated python file contains a method "def setupGui(self):". There was some duplication of effort. I solved the problem by removing duplicate code from setupGui method and some things I lost track of. Working code below:

gpkgpath = r'c:\tmp\DVD_surveying\NM\geopackages\nm_survey.gpkg'
class geoGcdb(QMainWindow, Ui_MainWindow):

  def __init__(self):
    QMainWindow.__init__(self)

    self.setupUi(self)
    self.setupGui()
    self.project = QgsProject()

    # Set the title for the app
    self.setWindowTitle("Survey Measurement Exploration and Integration Tools")

    self.add_ogr_layer(gpkgpath+'|layername=filedataset')

  def setupGui(self)
    self.map_canvas = QgsMapCanvas()                  
    self.grid_layout = QGridLayout(self.frame)        
    self.grid_layout.addWidget(self.map_canvas)       

  def add_ogr_layer(self, path):
    fdslayer = QgsVectorLayer(gpkgpath+'|layername=filedataset', 'file data set', 'ogr')
    if not fdslayer.isValid() :
      print('fdslayer NOT valid!!!')
      exit()
    self.project.addMapLayer(fdslayer)
    self.map_canvas.setExtent(fdslayer.extent())
    self.map_canvas.setLayers([fdslayer])
    self.map_canvas.show()

def main(argv):
  # create Qt application
  app = QApplication(argv)

  # Initialize qgis libraries
  QgsApplication.setPrefixPath(qgis_prefix, True)
  QgsApplication.initQgis()

  # create main window
  wnd = geoGcdb()
  # Move the app window to upper left
  wnd.move(100,100)
  wnd.size()
  wnd.show()
  # run!
  retval = app.exec_()

  # exit
  wnd = None
  QgsApplication.exitQgis()
  exit(retval)

if __name__ == "__main__":
  main(sys.argv)
1
  • how do you run this application? do you run it from DOS console like python youprogram.py? or are you running it in an IDE such as PyCharm? Aug 16 '18 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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