Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I created the simple frame .UI file, compiled with pyuic4, and tried the ShapeViewer.py to load the shapefile "world_borders.shp". However, the map doesn't display at the first time. I have to click out the window to make it appear. After everything works! I am working in Windows 7. Instalation "osgeo4w-setup-x86". Qgis 2.2.0-Valmiera

Why does QgsMapCanvas in Qgis 2.2.0-Valmiera only appear after losing focus?

Here is my code:

class ShapeViewer(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.canvas = QgsMapCanvas()
        self.canvas.useImageToRender(False)
        self.layout = QVBoxLayout(self.frame)
        self.layout.addWidget(self.canvas)
        file = QFileDialog.getOpenFileName(self,"Open", ".", "Shp(*.shp)")
        layer = QgsVectorLayer(file, "My Shape", "ogr")
        QgsMapLayerRegistry.instance().addMapLayer(layer);
        self.canvas.setExtent(layer.extent())
        cl = QgsMapCanvasLayer(layer)
        self.canvas.setLayerSet([cl])
        self.show()

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

    # Initialize qgis libraries
    QgsApplication.setPrefixPath(qgis_prefix, True)
    QgsApplication.initQgis()
    # create main window
    wnd = ShapeViewer()
    wnd.show()
    # run!
    retval = app.exec_()

    # exit
    QgsApplication.exitQgis()
    sys.exit(retval)
if __name__ == "__main__":
    main(sys.argv)
share|improve this question
    
This is interesting I am seeing this in a custom QGIS app I made too against 2.2. Not sure why yet. –  Nathan W Feb 26 at 21:33
    
@NathanW it's only happend in a custom QGIS app. I fixed this error by switching to version 2.0.1. I think it's a bug from 2.2. I'm not sure, because I have no experience in qgis. –  Marcelo Pacheco Apr 10 at 22:03
    
Yes it's a bug. There is a workaround. –  Nathan W Apr 11 at 3:21

4 Answers 4

This is a bug in 2.2 with the canvas and it's redraw event. The redraw event is called when the widget is first shown and it kicks off a timer which stops the canvas from painting.

The workaround is as simple as calling:

canvas.refresh()
canvas.repaint()

The first time the widget is shown.

share|improve this answer

I had the same issue. I solve it by calling to mainWindow.show after configuring the qgsmapcanvas. Hope it helps.

share|improve this answer
    
Barna, thx for your answer, but I did what you wrote but it doesn't work. –  Marcelo Pacheco Apr 2 at 15:14
up vote 0 down vote accepted

I fixed this error by switching to version 2.0.1 I think it's a bug from 2.2. I'm not sure, because I have no experience in qgis.

share|improve this answer

This bug cost me a day. The workaround is very specific. You must freeze and unfreeze the canvas at just the right time while also refreshing and repainting the canvas:

# Freeze the Canvas
# to make it immune to
# buggy events
canvas.freeze(True)
# Show the frozen canvas
canvas.show()
# Refresh the canvas content
canvas.refresh()
# Now, and only now, we can unfreeze
# the canvas
canvas.freeze(False)
# And finally, we can safely repaint it.
canvas.repaint()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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