Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Zoom functionality for Widget
QtWS25 Last Chance

Zoom functionality for Widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paxsentry
    wrote on last edited by
    #1

    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;
    
        ui->mapLabel->resize(sca);
        ui->mapLabel->repaint();
    }
    

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        paxsentry
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          paxsentry
          wrote on last edited by
          #4

          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 ***
           ui->mapLabel->setPixmap(QPixmap::fromImage(m_map));
          
          • 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);
              ui->mapLabel->repaint();
          }
          

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Which paint event is it ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • P Offline
              P Offline
              paxsentry
              wrote on last edited by paxsentry
              #6

              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Can you show the complete code ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved