QT界面,鼠标滚轮实现缩放问题
2014-11-13 11:15
1046 查看
1、实现放大与缩小接口:
//发生缩放
if(scaling_flag == 1)
{
Video->setScaledContents(false);
//fprintf(stderr,"__________________________%f\n",m_scaling);
//QImage: out of memory, returning null image
// QImage* imgScaled = new QImage;
// *imgScaled=mge.scaled(mge.width()*m_scaling,
// mge.height()*m_scaling,
// Qt::KeepAspectRatio);
// mge = *imgScaled;
//解决图片过大,出现内存溢出
mge = mge.scaled(mge.width()*m_scaling,
mge.height()*m_scaling,
Qt::KeepAspectRatio);
//fprintf(stderr,"----------------------------------%d,%d\n",mge.width(),mge.widthMM());
//解决图像中心缩放
if(mge.width()>Video->width())
{
mge = mge.copy((mge.width()-Video->width())/2,(mge.height()-Video->height())/2,
Video->width(),Video->height());
}
}
通过放大与缩小来改变m_scaling的值
void tomato::zoomIn()
{
if(m_videoDev.fd == -1)
{
QMessageBox::warning(this,"放大","摄像头未打开!");
return;
}
myTimer.stop();
scaling_flag = 1;
m_scaling = m_scaling*1.2;
while(m_scaling > 20)
{
m_scaling = 20;
}
myTimer.start();
}
void tomato::zoomOut()
{
if(m_videoDev.fd == -1)
{
QMessageBox::warning(this,"放大","摄像头未打开!");
return;
}
myTimer.stop();
scaling_flag = 1;
m_scaling = m_scaling/1.2;
while(m_scaling<0.05)
{
m_scaling = 0.05;
}
myTimer.start();
}
2、重写滚轮事件
// 滚轮事件
void tomato::wheelEvent(QWheelEvent *event)
{
/*
//得到鼠标事件的位置
//当滚动缩略图标时,鼠标位置不正确,导致图像也发生了缩放
int x = event->x();
int y = event->y();
*/
QPoint pos;
QPoint pos1;
QPoint pos2;
pos1 = mapToGlobal(QPoint(0,0));
pos2 = event->globalPos();
pos = pos2 - pos1;
/*
fprintf(stderr,"pos.x() = %d,pos.y() = %d;Video->x() = %d,Video->y() = %d;Video->width() = %d,Video->height() = %d\n"
,pos.x(),pos.y(),Video->x(),Video->y(),Video->width(),Video->height());
*/
//判断鼠标位置是否在图像显示区域
if (pos.x() > Video->x() && pos.x() < Video->x()+Video->width()
&& pos.y() > Video->y() && pos.y() < Video->y()+Video->height())
{
// 当滚轮远离使用者时进行放大,当滚轮向使用者方向旋转时进行缩小
if(event->delta() > 0)
{
zoomIn();
}
else
{
zoomOut();
}
}
}
//发生缩放
if(scaling_flag == 1)
{
Video->setScaledContents(false);
//fprintf(stderr,"__________________________%f\n",m_scaling);
//QImage: out of memory, returning null image
// QImage* imgScaled = new QImage;
// *imgScaled=mge.scaled(mge.width()*m_scaling,
// mge.height()*m_scaling,
// Qt::KeepAspectRatio);
// mge = *imgScaled;
//解决图片过大,出现内存溢出
mge = mge.scaled(mge.width()*m_scaling,
mge.height()*m_scaling,
Qt::KeepAspectRatio);
//fprintf(stderr,"----------------------------------%d,%d\n",mge.width(),mge.widthMM());
//解决图像中心缩放
if(mge.width()>Video->width())
{
mge = mge.copy((mge.width()-Video->width())/2,(mge.height()-Video->height())/2,
Video->width(),Video->height());
}
}
通过放大与缩小来改变m_scaling的值
void tomato::zoomIn()
{
if(m_videoDev.fd == -1)
{
QMessageBox::warning(this,"放大","摄像头未打开!");
return;
}
myTimer.stop();
scaling_flag = 1;
m_scaling = m_scaling*1.2;
while(m_scaling > 20)
{
m_scaling = 20;
}
myTimer.start();
}
void tomato::zoomOut()
{
if(m_videoDev.fd == -1)
{
QMessageBox::warning(this,"放大","摄像头未打开!");
return;
}
myTimer.stop();
scaling_flag = 1;
m_scaling = m_scaling/1.2;
while(m_scaling<0.05)
{
m_scaling = 0.05;
}
myTimer.start();
}
2、重写滚轮事件
// 滚轮事件
void tomato::wheelEvent(QWheelEvent *event)
{
/*
//得到鼠标事件的位置
//当滚动缩略图标时,鼠标位置不正确,导致图像也发生了缩放
int x = event->x();
int y = event->y();
*/
QPoint pos;
QPoint pos1;
QPoint pos2;
pos1 = mapToGlobal(QPoint(0,0));
pos2 = event->globalPos();
pos = pos2 - pos1;
/*
fprintf(stderr,"pos.x() = %d,pos.y() = %d;Video->x() = %d,Video->y() = %d;Video->width() = %d,Video->height() = %d\n"
,pos.x(),pos.y(),Video->x(),Video->y(),Video->width(),Video->height());
*/
//判断鼠标位置是否在图像显示区域
if (pos.x() > Video->x() && pos.x() < Video->x()+Video->width()
&& pos.y() > Video->y() && pos.y() < Video->y()+Video->height())
{
// 当滚轮远离使用者时进行放大,当滚轮向使用者方向旋转时进行缩小
if(event->delta() > 0)
{
zoomIn();
}
else
{
zoomOut();
}
}
}
相关文章推荐
- QT界面,鼠标滚轮实现缩放问题
- delphi中使用mapx想用鼠标滚轮实现自定义倍率的缩放,并且限制一定的缩放倍数
- 鼠标滚轮实现图片的缩放
- 鼠标滚轮实现图片缩放
- 在C#中实现图片缩放(含asp.net用鼠标滚轮实现图片缩放)
- Engine中如何实现鼠标滚轮缩放反置?
- 如何去掉鼠标滚轮缩放图片和解决签名图片变形问题。
- 在unity中用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放
- 在unity中用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放
- jquery 鼠标滚轮实现图片缩放
- js实现鼠标滚轮滚动以当前鼠标焦点为中心缩放
- unity3D 鼠标滚轮实现物体的大小缩放
- jQuery 鼠标滚轮实现图片缩放
- js实现鼠标滚轮控制图片缩放效果的方法(转)
- asp.net用鼠标滚轮实现图片缩放实现方法
- 鼠标滚轮实现图片的缩放-------Day79
- Qt实现360安全卫士10.0界面(编译时出现的一些问题)
- 在unity中用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放
- js实现鼠标滚轮控制图片缩放效果的方法
- Qt拖拽界面 (*.ui) 缩放问题及解决办法