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. Qt zoom with custom QWidget
Forum Updated to NodeBB v4.3 + New Features

Qt zoom with custom QWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 5.1k Views 3 Watching
  • 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    Hey folks,

    I have a custom subclass of a QWidget that I use as the centralWidget of a QMainWindow. I'd like to be able to zoom in and out of that widget. Is there any mechanism present already in Qt that allows me to do that? I know that QScrollArea provides exactly that functionality for panning around - I'd just like to have the same ability but with the additional ability to zoom in and out.

    If there's no existing easy solution to do this, what do I need to do in order to implement this anyway? How complex will it be? Do I simply need to implement my own scale factor and scale in paintEvent() accordingly? I assume sizeHint() would need to take that into account as well. Anything else to consider?

    As always: I appreciate any help! :)

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    C 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      what are you zooming ?
      Using painter and
      http://doc.qt.io/qt-5/qpainter.html#scale
      might be an option.

      1 Reply Last reply
      2
      • Joel BodenmannJ Joel Bodenmann

        Hey folks,

        I have a custom subclass of a QWidget that I use as the centralWidget of a QMainWindow. I'd like to be able to zoom in and out of that widget. Is there any mechanism present already in Qt that allows me to do that? I know that QScrollArea provides exactly that functionality for panning around - I'd just like to have the same ability but with the additional ability to zoom in and out.

        If there's no existing easy solution to do this, what do I need to do in order to implement this anyway? How complex will it be? Do I simply need to implement my own scale factor and scale in paintEvent() accordingly? I assume sizeHint() would need to take that into account as well. Anything else to consider?

        As always: I appreciate any help! :)

        C Offline
        C Offline
        Charlie_Hdz
        wrote on last edited by
        #3

        @Joel-Bodenmann

        Just to clarify, Do you want to zoom what's inside the Widget? Or do you want to increment the size of the "Widget"?

        They're two different things.

        Kind Regards,

        Enrique

        Kind Regards,
        Enrique Hernandez
        gearstech.com.mx
        chernandez@gearstech.com.mx

        1 Reply Last reply
        1
        • Joel BodenmannJ Offline
          Joel BodenmannJ Offline
          Joel Bodenmann
          wrote on last edited by
          #4

          I want to make the widget become bigger. The content stays the same, but the content needs to scale with the widget.

          Here's my widget's paintEvent():

          void Foo::paintEvent(QPaintEvent* event)
          {
              QPainter painter(this);
          
              // Render the µGFX image
              pixel_t* pixmapSurface = gdispPixmapGetBits(_display);
              QImage img((const uchar*)pixmapSurface, width(), height(), width()*sizeof(color_t), QImage::Format_RGB32);
              painter.drawImage(event->rect(), img, img.rect());
          
              // Paint a boarder
              {
                  // Border pen
                  QPen borderPen;
                  borderPen.setStyle(Qt::SolidLine);
                  borderPen.setColor(Qt::black);
                  borderPen.setWidth(1);
          
                  // Border brush
                  QBrush borderBrush;
                  borderBrush.setStyle(Qt::NoBrush);
          
                  // Draw the border
                  qreal w = borderPen.width()/2.0;
                  painter.setPen(borderPen);
                  painter.setBrush(borderBrush);
                  painter.drawRect(event->rect().adjusted(w, w, -2*w, -2*w));
              }
          }
          

          As you can see it basically just renders a QImage with a border. So If the widget is 200 by 100 size and I apply a scale of 200% then I'd like to have the widget being 400 by 200 and the image inside of it would be scaled to two times the size as well.

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          C 1 Reply Last reply
          0
          • Joel BodenmannJ Joel Bodenmann

            I want to make the widget become bigger. The content stays the same, but the content needs to scale with the widget.

            Here's my widget's paintEvent():

            void Foo::paintEvent(QPaintEvent* event)
            {
                QPainter painter(this);
            
                // Render the µGFX image
                pixel_t* pixmapSurface = gdispPixmapGetBits(_display);
                QImage img((const uchar*)pixmapSurface, width(), height(), width()*sizeof(color_t), QImage::Format_RGB32);
                painter.drawImage(event->rect(), img, img.rect());
            
                // Paint a boarder
                {
                    // Border pen
                    QPen borderPen;
                    borderPen.setStyle(Qt::SolidLine);
                    borderPen.setColor(Qt::black);
                    borderPen.setWidth(1);
            
                    // Border brush
                    QBrush borderBrush;
                    borderBrush.setStyle(Qt::NoBrush);
            
                    // Draw the border
                    qreal w = borderPen.width()/2.0;
                    painter.setPen(borderPen);
                    painter.setBrush(borderBrush);
                    painter.drawRect(event->rect().adjusted(w, w, -2*w, -2*w));
                }
            }
            

            As you can see it basically just renders a QImage with a border. So If the widget is 200 by 100 size and I apply a scale of 200% then I'd like to have the widget being 400 by 200 and the image inside of it would be scaled to two times the size as well.

            C Offline
            C Offline
            Charlie_Hdz
            wrote on last edited by
            #5

            @Joel-Bodenmann

            What are the results so far?

            You're confusing the terms of QPaint and Widget.

            Using the Painter is to modify the render "inside" the Widget. To modify the size of the widget is different.

            set the Height and Width of the widget in a slot connected to your scroll bar.

            Kind Regards,

            Enrique

            Kind Regards,
            Enrique Hernandez
            gearstech.com.mx
            chernandez@gearstech.com.mx

            1 Reply Last reply
            0
            • Joel BodenmannJ Offline
              Joel BodenmannJ Offline
              Joel Bodenmann
              wrote on last edited by
              #6

              I think there was some misunderstanding here. Everything is working. I implemented the zooming myself.
              My question was more like: Does Qt provide a default container that would already have done that for me? Similar to like QScrollArea provides panning capabilities to an existing QWidget without modifying said QWidget.

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              mrjjM 1 Reply Last reply
              0
              • Joel BodenmannJ Joel Bodenmann

                I think there was some misunderstanding here. Everything is working. I implemented the zooming myself.
                My question was more like: Does Qt provide a default container that would already have done that for me? Similar to like QScrollArea provides panning capabilities to an existing QWidget without modifying said QWidget.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Joel-Bodenmann
                Not for widgets. The Graphics framework have zooming for its items.

                1 Reply Last reply
                3

                • Login

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