Zooming detail and resolution on QGraphicsView

  • Hello,

    I'm having some problems using the Graphics Framework to have a smooth zoom and resolution when zoomed in on a QImage.
    So far I have my GraphicsView that has a scene and an Item. In my wheel event, I calculate a factor according to how much the wheel is moved, and then using QGraphicsView::scale(factor,1.0) the image is "zoomed". Now this works fine except that I need to see the detail as best as possible of the image when zoomed-in, so the scale function of the view just scales the image, therefore the image is stretched and distorted.

    So I think I need to save different sizes of the image to show depending how zoomed I am in the view. My initial idea is doing something like this in the wheel event function of my GraphicsView class

    void MyView::wheelEvent(QWheelEvent *event)
    float exp = (event->delta() / 240.0);
    float factor = powf(1.41, exp);

    Q_EMIT zoom(factor);  //goes to a slot that will scale the view with the factor value 
        if(factor > 1.5)
            Q_EMIT updateImageSig(factor); //will go to a slot to get the new size images and set them to display    


    void m_MainWindow::updateImageSLOT(float factor)
    QImage image = getImage(factor); //function that generates the images with different sizes
    myview->getItem()->setImage(image); //function that will set the image in the item to display

    At first my QImage gets created in a function as:

    myImage = new QImage(X, Y, QImage::Format_Indexed8);

    Where X is 1000 and Y is 400. So in the getImage(factor) function above I was thinking of doing something like:

    myImage = new QImage(X / factor, Y/factor, QImage::Format_Indexed8);

    The paint function in my item class for now is doing :
    void GraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

     painter->drawImage(boundingRect(), itemImage);     //itemImage gets passed to the item 


    I tried to be as detailed as possible, if anyone can help me get on the right track I'll really appreciate it. My doubts are how could I keep in memory several version of my image to use when needed. Am I at least going in the right direction ?
    I also saw something about the option->levelOfDetail in the paint function of the item but I'm not sure it could help me in this case..?
    Thanks for the help in advance!

  • Hi,

    Not sure if this is possible for your use case, but have you thought of using a vector graphic image (SVG)? This would save you having to create different sizes of images and your paint could just redraw the svg at the required resolution.

    The rendering of SVG is slow, but depending on your application it might work.


  • Yes I have actually considered it, but didn't try it out yet since I'm not familiar with the QSvg Module yet. I don't know if just changing the format of the image will do the trick though since I am trying to get fine detail when zoomed in my image.

  • Are they images your loading from disk or ones you're creating in code? If they're from disk then you can use a tool like Inkscape to generate them, but if you're doing it from code I wouldn't know where to start!

    Just changing the format won't quite do it all, you will still have to redraw the vector image to the new scale, but that should just be a simple QImage call. I don't think just zooming in the graphics view will be enough as it will probably just scale the pre-drawn image.

Log in to reply

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