[Qgis-developer] setting QgsMapCanvas size, for testing map tools

Martin Dobias wonder.sk at gmail.com
Fri Jul 1 07:15:44 PDT 2016


Hi Tom!

On Fri, Jul 1, 2016 at 1:08 PM, Tom Chadwin <tom.chadwin at nnpa.org.uk> wrote:
> Barry Rowlingson wrote
>> Maybe I've missed something here, but doesn't the canvas resize method
>> set the canvas pixel size? This behaves as expected when run
>> standalone from a python prompt.
>>
>> from qgis.core import *
>> from qgis.gui import *
>> app = QgsApplication([],True)
>> canvas = QgsMapCanvas()
>> canvas.show()
>> canvas.resize(1000,200)
>> canvas.resize(1000,1000)
>>
>> Note the map canvas has no parent, so just appears on its own at the
>> specified size.
>>
>> Or maybe I've missed a point....
>
> I don't want to misrepresent what Sandro is asking, but I think it's the
> same as me. I think (and this is from memory), that we need to do this to
> the QGIS app canvas instance, rather than a newly created one.
>
> When I run canvas.resize() locally, it behaves as I expect. I can then run
> qgis2web (this is my reason for wanting to solve this), to export a web map
> of the same size as the canvas.
>
> What I don't understand is that when testing under Travis, with the same
> canvas.resize() in the test script, I get different bounds in the webmap to
> what I get when resizing my client canvas and exporting.

I guess the problem lies in the fact that canvas widget has a frame at
its edge (it is drawn within the area of canvas widget). There may be
differences between the "internal" area of canvas due to different Qt
styles being used. To see the outer vs inner measurements:

>>> iface.mapCanvas().geometry()
PyQt4.QtCore.QRect(0, 0, 879, 390)
>>> iface.mapCanvas().viewport().geometry()
PyQt4.QtCore.QRect(3, 3, 873, 384)

Probably what you need to do is to remove the frame altogether:

>>> iface.mapCanvas().setFrameStyle(0)

Then you get:

>>> iface.mapCanvas().geometry()
PyQt4.QtCore.QRect(0, 0, 879, 390)
>>> iface.mapCanvas().viewport().geometry()
PyQt4.QtCore.QRect(0, 0, 879, 390)

Hope that helps!

Cheers
Martin


More information about the Qgis-developer mailing list