Hi,

In my Qt code I have added a grid layout in my tabwidget statically(i.e., in the form itself). Then I have added QScrollArea in my grid layout dynamically, and then I have added a Qwidget in my QScrollArea dynamically again(i.e. in the constructor). In my Qwidget I am adding a .jpg image. In my wheelevent I am resizing the image as well as Qwidget to give the zooming effect. In addition, in my wheel event I have also added a condition to check whether the cursor is inside the grid layout or not. But in doing this I am having problems. When I zoom the image once, everything works fine. But once image size becomes larger than QScrollArea, zooming code stops working. In debugging I came to know that after zooming once, when I get the cursor over the tab widget, the co-ordinates of the cursor show undesirable values which is not the position of the mouse, due to which condition in the Wheel event is not satisfied and resizing of the image and Qwidget does not happen. Why is this happening?? Is resizing of the Qwidget making problem??
What is the solution to this??

Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "QScrollArea"
  4. #include "QScrollBar"
  5.  
  6. #define PROCESS_FRAME_WIDTH (368)
  7. #define PROCESS_FRAME_HEIGHT (276)
  8.  
  9. MainWindow::MainWindow(QWidget *parent) :
  10. QMainWindow(parent),
  11. ui(new Ui::MainWindow)
  12. {
  13. ui->setupUi(this);
  14. scrollArea_Car = new QScrollArea;
  15. scrollArea_Car->setBackgroundRole(QPalette::Dark);
  16. scaleFactor = 1.0;
  17. QTimer::singleShot(0, this, SLOT(myThread()));
  18. }
  19.  
  20. void MainWindow::myThread()
  21. {
  22. myframewidget = new IplImageWidget(this);
  23. frame_widget_image = cvLoadImage("myimageunavailable.png",CV_LOAD_IMAGE_COLOR);
  24.  
  25. /* update frame widget */
  26. ui->frame_image->addWidget(scrollArea_Car,0,0,1,1,0);
  27. myframewidget->setIplImage(frame_widget_image);
  28. scrollArea_Car->setWidget(myframewidget);
  29. myframewidget->show();
  30. myframewidget->adjustSize();
  31.  
  32. x_start_car = ui->frame->x() + ui->groupBox_2->x() + myframewidget->x();
  33. y_start_car = ui->frame->y() + ui->groupBox_2->y() + myframewidget->y();
  34. x_end_car = ui->frame->x() + ui->groupBox_2->x() + myframewidget->x() + myframewidget->width();
  35. y_end_car = ui->frame->y() + ui->groupBox_2->y() + myframewidget->y() + myframewidget->height();
  36.  
  37. }
  38.  
  39. void MainWindow::wheelEvent(QWheelEvent *event)
  40. {
  41. if(((event->pos().x() >= x_start_car) && (event->pos().x() <= x_end_car)) && ((event->pos().y() >= y_start_car) && (event->pos().y() <= y_end_car)))
  42. {
  43. int numDegree = event->delta() / 8;
  44. double numStep = numDegree / 15.0f ;
  45. numStep = pow(0.95f,numStep);
  46. scaleImage(numStep);
  47. }
  48. }
  49.  
  50. void MainWindow::scaleImage(double factor)
  51. {
  52. Q_ASSERT(myframewidget);
  53. scaleFactor *= factor;
  54.  
  55. myframewidget->resize(scaleFactor * PROCESS_FRAME_WIDTH,scaleFactor * PROCESS_FRAME_HEIGHT);
  56.  
  57. IplImage* global_image_car_grey = cvCreateImage(cvSize(global_image_car->width,global_image_car->height),IPL_DEPTH_8U,1);
  58. IplImage* global_image_car_resized = cvCreateImage(cvSize(scaleFactor * PROCESS_FRAME_WIDTH,scaleFactor * PROCESS_FRAME_HEIGHT),IPL_DEPTH_8U,1);
  59. cvCvtColor(global_image_car,global_image_car_grey,CV_RGB2GRAY);
  60.  
  61. cvResize(global_image_car_grey,global_image_car_resized,CV_INTER_LINEAR);
  62. myframewidget->setIplImage(global_image_car_resized);
  63. myframewidget->show();
  64. cvReleaseImage(&global_image_car_resized);
  65. cvReleaseImage(&global_image_car_grey);
  66.  
  67. adjustScrollBar(scrollArea_Car->horizontalScrollBar(), factor);
  68. adjustScrollBar(scrollArea_Car->verticalScrollBar(), factor);
  69. }
To copy to clipboard, switch view to plain text mode