[QGIS-Developer] Convert from mapunits to pixels

Nyall Dawson nyall.dawson at gmail.com
Fri Nov 10 15:50:57 PST 2017


On 10 November 2017 at 02:26, Tom Chadwin <tom.chadwin at nnpa.org.uk> wrote:
> Hello all
>
> I've never implemented support of any unit other than pixels in my plugin. A
> particular use case (vector tiles) could really use mapunits. Can someone
> explain (or point me towards) the method for converting from mapunits to
> pixels for on-screen rendering?

Have you got access to a QgsRenderContext? (If not, you can create one
from a QgsMapSettings by QgsRenderContext::fromMapSettings)

After you've got the render context you can use

- QgsRenderContext::convertFromMapUnits to convert from map units to
any other unit (including pixels, mm, etc)
- QgsRenderContext::convertToMapUnits to convert from any unit
(including pixels and mm) to map units

(That's 3.0 API)

Nyall


More information about the QGIS-Developer mailing list