Important: Please read the Qt Code of Conduct -

Zoom functionality for Widget

  • Hi,

    While working on a simple application, I have a main view which contains a ScrollArea in vertical layout and a widget in it.
    This widget is responsible for displaying a map - as the main feature of the application.
    What I'd like to do is implement the zoom in/out functionality for this map display.

    Done so far:

    • tried to scale the widget from the main view -> got weird results
    • found the Image Viewer example, created a QLabel, but on zoom it blinks only once and nothing happens.

    Functions used:
    paintEvent - responsible to draw the grid for the map, also got a QImage in it and in the end of drawing everything this image is used in setPixmap(QPixmap::fromImage(map));
    zoom - the auto properties just for debugging

    void MapDisplay::ZoomIn(double factor)
        m_scale *= factor;
        auto size = ui->mapLabel->size();
        auto sca = m_scale * size;

    Any help appreciated or if you need more code can give you the repo address too, which contains the whole app.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should rather resize the image and set it again on the QLabel but that won't necessarily end with a high quality map.

    Why not use the QtLocation module ?

  • Thank you, will try the image resize.
    Sorry, forgot to mention, this map is a game map not an actual one :)

  • Not sure what is wrong, but still no zoom :/
    This is what I have done:

    • added a QImage to the class (m_map)
    • in the beginning of the paintEvent: (m_mapSize is selected by the user)
    m_map = QImage(m_mapSize, m_mapSize, QImage::Format_ARGB32);
    QPainter renderer(&m_map)
    *** more drawing ***
    • changed the ZoomIn function
    void MapDisplay::ZoomIn(double factor)
        m_scale *= factor;
        auto size = m_map.size();
        auto sca = m_scale * size;
        m_map = m_map.scaled(sca);

    In the above call the m_map is the correct size, the scale executed, also the repaint, but nothing changed.

  • Lifetime Qt Champion

    Which paint event is it ?

  • This is the MapDisplay paint event, which is responsible to draw all lines and map tiles.
    The MainView contains the ScrollArea, which is working fine if the map is bigger than the widget size.

  • Lifetime Qt Champion

    Based only on your code, it's not clear how your widgets are working.

    Can you show the complete code ?

Log in to reply