[Qgis-developer] setting QgsMapCanvas size, for testing map tools
Martin Dobias
wonder.sk at gmail.com
Fri Jul 1 09:47:16 PDT 2016
Hi Sandro
On Fri, Jul 1, 2016 at 6:07 PM, Sandro Santilli <strk at kbt.io> wrote:
> On Fri, Jul 01, 2016 at 05:58:57PM +0200, Sandro Santilli wrote:
>> On Thu, Jun 30, 2016 at 10:14:34PM +0100, 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.
>>
>> It does ! Thanks for the pointer.
>
> Still, my problem is the following:
>
> QgsMapCanvas *mCanvas = new QgsMapCanvas();
> mCanvas->resize( QSize(50, 50) );
> mCanvas->setExtent( QgsRectangle( 0, 0, 50, 50 ) );
> QCOMPARE( mCanvas->scale(), 1.0 ); //<-- fails, returning 3.1631e+07
I think what you are up to is not to find out scale(), but actually
mapUnitsPerPixel(). Scale in QGIS is calculated as ( extent_width / (
canvas_width * dpi) ) with some extra conversions involved, see
QgsScaleCalculator class.
Try this in PyQGIS console:
>>> cx = QgsMapCanvas()
>>> cx.setFrameStyle(0)
>>> cx.resize(QSize(100,100))
>>> cx.setExtent(QgsRectangle(0,0,100,100))
>>> cx.show()
>>> cx.mapSettings().outputSize()
PyQt4.QtCore.QSize(100, 100)
>>> cx.mapSettings().mapUnitsPerPixel()
1.0
One caveat here is the fact you need to call show() method of canvas,
otherwise the resize event (that sets image output size) will not be
fired and thus the image output size not updated.
Cheers
Martin
More information about the Qgis-developer
mailing list