2

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);

}

2
  • Why are you using adjustSize() twice?
    – red1ynx
    Jun 14, 2011 at 11:46
  • its just a mistake. I got angry when it didnt work.
    – tkcn
    Jun 14, 2011 at 11:57

2 Answers 2

1

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

0

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.