我有一个应用,我想缩放光标位置。现在,它将放大图像的中心。 注意1:我使用pos.x()和pos.y()确定并打印光标的位置。 注意2:scaleImage(0.25)表示放大25%。 这是放大功能:
void ImageView::zoomIn()
{
QPoint pos = QCursor::pos();
printf("pos: %d, %d \n", pos.x(), pos.y());
if(m_scaleFactor >= 12)
return;
if (m_visibleImg.size() == m_image.size() &&
(m_scrollArea->horizontalScrollBar()->pageStep() * 3 < m_scrollArea->horizontalScrollBar()->maximum() ||
m_scrollArea->verticalScrollBar()->pageStep() * 3 < m_scrollArea->verticalScrollBar()->maximum()) &&
m_scaleFactor >= 2.25)
{
int maxh = m_scrollArea->horizontalScrollBar()->maximum();
int maxv = m_scrollArea->verticalScrollBar()->maximum();
int valh = m_scrollArea->horizontalScrollBar()->value();
int valv = m_scrollArea->verticalScrollBar()->value();
m_numImage = selectImage(maxh, maxv, valh, valv);
m_imageLabel->setNumImg(m_numImage);
m_visibleImg = m_image.copy(m_vectDimImg.at(m_numImage));
double Hscale = INT_MAX;
if(valh != 0)
Hscale = maxh / valh;
double Vscale = INT_MAX;
if(valv != 0)
Vscale = maxv / valv;
scaleImage(0.25);
m_scrollArea->horizontalScrollBar()->setValue(m_scrollArea->horizontalScrollBar()->maximum() / Hscale);
m_scrollArea->verticalScrollBar()->setValue(m_scrollArea->verticalScrollBar()->maximum() / Vscale);
}
else
{
scaleImage(0.25);
}
}
scaleImage函数如下所示:
void ImageView::scaleImage(double factor, bool keepScroll)
{
QPoint pos = QCursor::pos();
m_scaleFactor += factor;
int maxH = m_scrollArea->horizontalScrollBar()->maximum();
int maxV = m_scrollArea->verticalScrollBar()->maximum();
//if(m_visibleImg.size() )
//m_visibleImg = m_image.copy(m_vectDimImg.at(m_numImage));
if(m_visibleImg.isNull())
return;
QImage tmp = m_visibleImg;
if(!m_imageLabel->getVideoIsPlaying())
lightAdjust(tmp, m_lightFactor);
QPixmap pix = QPixmap::fromImage(tmp);
m_imageLabel->setPixmap(pix.scaled(m_scaleFactor * pix.size()));
// m_imageLabel->resize(m_imageLabel->pixmap()->size());
m_imageLabel->adjustSize();
if(keepScroll)
{
m_scrollArea->horizontalScrollBar()->setValue(m_scrollHorValue);
m_scrollArea->verticalScrollBar()->setValue(m_scrollVerValue);
}
else if(factor != 0)
{
adjustScrollBar(m_scrollArea->horizontalScrollBar(), maxH, factor);
adjustScrollBar(m_scrollArea->verticalScrollBar(), maxV, factor);
}
m_zoomInAct->setEnabled(m_scaleFactor < 12.0);
m_zoomOutAct->setEnabled(m_scaleFactor > 0.25);
}
我将不胜感激! :)