24,855
社区成员
发帖
与我相关
我的任务
分享
private slots:
// Reimplement the wheelEvent() to Achieve Zoom in and Zoom out.
void wheelEvent(QWheelEvent *e);
void MatchWindowSingle::wheelEvent(QWheelEvent *e)
{
// If numDegress > 0, then zoom in,
// else, zoom out.
// The scale control the picture's size.
int numDegress = e->delta();
// Update ScrollArea 下面是执行图片缩放的函数代码
if (ui.sa_geoImage->hasFocus()){
identityScaleFactor(&numDegress, &scaleFactor_MIN_g, &scaleFactor_g);
updateScrollArea(&geoPic, ui.sa_geoImage, &geoImageLable, &scaleFactor_g);
}else if (ui.sa_videoImage->hasFocus()){
identityScaleFactor(&numDegress, &scaleFactor_MIN_v, &scaleFactor_v);
updateScrollArea(&videoPic, ui.sa_videoImage, &videoImageLable, &scaleFactor_v);
}
}
就是通过viewport()返回QScrollArea的Viewport属性,然后为这个东西添加事件过滤器而不是直接把过滤器添加到scrollArea上。
首先,wheelEvent()并不是QScrollArea本身的虚函数,它是从QWiget中继承来的方法;当初重写时,既没有重写QScrollArea,也没有考虑到它通过QScrollArea::setWiget()所设置的QLabel部件,而是重写了QScrollArea的父窗体MainWindowSingle;
然后,为了解除QScrollArea对wheelEvent的控制,我们重写MainWindowSingle::wheelEvent,或是使用事件过滤器都可以,前提是要解除QScrollArea中QLabel对wheelEvent的控制,因为QScrollArea本身并没有控制到wheelEvent的纵向滚动。
综上,问题中的代码可以类似的改写如下;当然使用eventFilter也是一样的:
QLabel *pGeoImgLabel = new QLabel;
QLabel *pVideoImgLabeo = new QLabel;
ui.sa_geoImage->setWidget(pGeoImgLabel);
ui.sa_geoImage->setWidget(pVideoImgLabeo);
private slots:
// Reimplement the wheelEvent() to Achieve Zoom in and Zoom out.
void wheelEvent(QWheelEvent *e);
void MatchWindowSingle::wheelEvent(QWheelEvent *e)
{
// If numDegress > 0, then zoom in,
// else, zoom out.
// The scale control the picture's size.
int numDegress = e->delta();
// Update ScrollArea 下面是执行图片缩放的函数代码
if (pGeoImgLabel->hasFocus()){
identityScaleFactor(&numDegress, &scaleFactor_MIN_g, &scaleFactor_g);
updateScrollArea(&geoPic, ui.sa_geoImage, &geoImageLable, &scaleFactor_g);
return true;
}else if (pVideoImgLabel->hasFocus()){
identityScaleFactor(&numDegress, &scaleFactor_MIN_v, &scaleFactor_v);
updateScrollArea(&videoPic, ui.sa_videoImage, &videoImageLable, &scaleFactor_v);
return true;
}
}
