0

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)
3
  • 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, 2014 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. Apr 10, 2014 at 22:03
  • Yes it's a bug. There is a workaround.
    – Nathan W
    Apr 11, 2014 at 3:21

4 Answers 4

1

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.

0

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

1
  • Barna, thx for your answer, but I did what you wrote but it doesn't work. Apr 2, 2014 at 15:14
0

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.

0

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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