Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created the simple frame .UI file, compiled with pyuic4, and tried the 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):
        self.canvas = QgsMapCanvas()
        self.layout = QVBoxLayout(self.frame)
        file = QFileDialog.getOpenFileName(self,"Open", ".", "Shp(*.shp)")
        layer = QgsVectorLayer(file, "My Shape", "ogr")
        cl = QgsMapCanvasLayer(layer)

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

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

    # exit
if __name__ == "__main__":
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 '14 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 '14 at 22:03
Yes it's a bug. There is a workaround. – Nathan W Apr 11 '14 at 3:21

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:


The first time the widget is shown.

share|improve this answer

I had the same issue. I solve it by calling to 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 '14 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
# Show the frozen canvas
# Refresh the canvas content
# Now, and only now, we can unfreeze
# the canvas
# And finally, we can safely repaint it.
share|improve this answer

Your Answer


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.