Question

I'm trying to zoom in a picture using QT and C++.

I inherited QLabel objects in my classes to show pictures. And also put this QLabels in mdiarea.

zoom function is working fine however the qlabel does not update its size immediately. If I try to resize is manually (with cursor) program automatically handles it and resize the qlabel as I wish.

How can I update the sizes immediately.

Thanks for help. :)

bool MdiChild::event ( QEvent * e ){
    //qDebug("asd1");
if(e->type() == QEvent::Wheel){
    int numDegrees = ((QWheelEvent*) e)->delta() / 8;
    double numSteps = (double)numDegrees/150;
    int w = pix->width();
    int h = pix->height();
    ratio += numSteps;
    qDebug("ratio = %f", ratio);
    QPixmap* p = new QPixmap(pix->scaledToHeight ( (int)(h * ratio),Qt::FastTransformation  ));
    setPixmap(*p);
    adjustSize();
    adjustSize();
    update();

}
return QWidget::event(e);

 } 

The problem is resolved but I think I cannot answer my own question. When I add the same event to the parent window problem is solved. But when I maximize the window the inner object also got the event and crashes the maximized window.

 bool ImageProcessor::event ( QEvent * e ){
if(e->type() == QEvent::Wheel){
    QList<QMdiSubWindow *> childList = ui.mdiArea->subWindowList();
    for(int i = 0; i<childList.count(); i++){
        childList[i]->adjustSize();
    }
}
return QWidget::event(e);

}

Was it helpful?

Solution 2

The problem is resolved but I think I cannot answer my own question. When I add the same event to the parent window problem is solved. But when I maximize the window the inner object also got the event and crashes the maximized window.

bool ImageProcessor::event ( QEvent * e ){
if(e->type() == QEvent::Wheel){
    QList<QMdiSubWindow *> childList = ui.mdiArea->subWindowList();
    for(int i = 0; i<childList.count(); i++){
        childList[i]->adjustSize();
    }
}
return QWidget::event(e);
}

OTHER TIPS

You need a QScrollArea to hold your QLabel. Otherwise when the window resizes your QLabel will not have scrollbars.

Look at the examples to see how to create an image viewer and how they resize.

ImageViewer example

Zoomable Picture Viewer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top