Restrict resize of a Widget.



  • I have Image viewer with some navigation controls.
    0_1546518679742_ImageViewer.jpg
    So, in QGraphicsView there's a QGraphicsScene where I set the image by pixmap.
    Based on the pixmaps width and height, I have restricted the min size of the QGraphicsView as well as the whole widget.
    Now increasing the size of the main widget, it should allow to resize if the width : height ratio matches the image's aspect ratio and the image will scale.
    Let's say my image is 640 : 480, so ratio is 1.3333. Now when I try to resize my ImageViewer, the QGraphicsViewer's new size should have the ratio of 1.333, else the ImageViewer shouldn't resize.
    So, I have overriden resizeEvent(QResizeEvent *event) of the ImageViewer, but I couldn't restrict the resize only on valid conditions. This resizeEvent(QResizeEvent *event) gets called whenever I drag the corners.

    Any suggestions are appreciated!!

    Regards,
    Sayan


  • Qt Champions 2017

    You can try using the event filters and fix the size once it reaches certain limit.


  • Lifetime Qt Champion

    Hi,

    If you want to have a specific aspect ratio, then you should rather handle the resizing in a container widget that would have the ImageViewer as member variable.



  • @SGaist I tried your suggestion, but the resize event gets called after the widget has resized.

    I tried the below, but it fails on start only,-> no widget is visible..it takes size 0,0..so

    double ratio = "1.3";
    double windowRatio = (double) width() / height();
    if(ratio == s)
    {
       QMdiSubWindow::resizeEvent(event);
    }
    else
    {
       resize(event->oldSize());
    }
    


  • @dheerendra Sir can you explain a bit more in detail.
    Are you asking to installEventFilters for resize event?


  • Lifetime Qt Champion

    The idea is that you have your container widget getting resized and in its resize event, you change the size of the inner widgets with the ratio you want.