700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > QT界面 鼠标滚轮实现缩放问题

QT界面 鼠标滚轮实现缩放问题

时间:2020-12-11 03:20:30

相关推荐

QT界面 鼠标滚轮实现缩放问题

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

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。