Qt zoom with custom QWidget



  • 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! :)


  • Qt Champions 2016

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



  • @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



  • 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.



  • @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



  • 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.


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.