CustomGraphicsItem::CustomGraphicsItem()
{
QGraphicsItem::setCacheMode(QGraphicsItem::NoCache);
QGraphicsItem::setAcceptDrops(true);//If enabled is true, this item will accept hover events; otherwise, it will ignore them. By default, items do not accept hover events.
m_scaleValue = 1;
m_scaleDafault = 1;
m_isMove = false;
m_hasPix=false;
wid=0;
hei=0;
}
QRectF CustomGraphicsItem::boundingRect() const
{
float w=1024;
float h=1040;
if (m_hasPix){
return QRectF(-wid / 2, -hei / 2, wid, hei);
}else{
return QRectF(-w/2,-h/2, w, h);
}
}
void CustomGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
if (m_hasPix){
// qDebug() << "m_scaleValue"<<m_scaleValue;
// qDebug() <<"m_pix"<<m_pix.size();
wid=m_pix.width();
hei=m_pix.height();
QPixmap scaledPixmap = m_pix.scaled(wid*m_scaleValue, hei*m_scaleValue);
wid=scaledPixmap.width();
hei=scaledPixmap.height();
// qDebug() << scaledPixmap.size();
painter->drawPixmap(-wid / 2, -hei / 2, scaledPixmap);
}
}
void CustomGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// qDebug() << "left";
if(event->buttons() & Qt::LeftButton)
{
m_startPos = event->pos();//鼠标左击时,获取当前鼠标在图片中的坐标,
m_isMove = true;//标记鼠标左键被按下
QApplication::setOverrideCursor(Qt::OpenHandCursor); //设置鼠标样式
}
// else if(event->buttons() & Qt::RightButton)
// {
// m_contextMenu->exec(QWidget::cursor().pos());
// //ResetItemPos();//右击鼠标重置大小
// /*if (this->QWidget::hasFocus()) {
// }*/
// }
}
void CustomGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// qDebug() << "move";
if(m_hasPix)
{
QPointF point = (event->pos() - m_startPos)*m_scaleValue; moveBy(point.x(), point.y());
}
}
void CustomGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
m_isMove = false;//标记鼠标左键已经抬起
QApplication::setOverrideCursor(Qt::ArrowCursor); //改回鼠标样式
}
void CustomGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event)//鼠标滚轮事件
{
qDebug() << "wheel"<<m_scaleValue;
if((event->delta() > 0)&&(m_scaleValue >= 2))//最大放大到原始图像的50倍
{
ResetItemPos();
}
// else if((event->delta() < 0)&&(m_scaleValue <= m_scaleDafault))//图像缩小到自适应大小之后就不继续缩小
// {
// ResetItemPos();//重置图片大小和位置,使之自适应控件窗口大小
// }
else
{
qreal qrealOriginScale = m_scaleValue;
if(event->delta() > 0)//鼠标滚轮向前滚动
{
m_scaleValue*=1.1;//每次放大10%
}
else
{
m_scaleValue*=0.9;//每次缩小10%
}
// setScale(m_scaleValue);
if(event->delta() > 0)
{
moveBy(-event->pos().x()*qrealOriginScale*0.1, -event->pos().y()*qrealOriginScale*0.1);//使图片缩放的效果看起来像是以鼠标所在点为中心进行缩放的
}
else
{
moveBy(event->pos().x()*qrealOriginScale*0.1, event->pos().y()*qrealOriginScale*0.1);//使图片缩放的效果看起来像是以鼠标所在点为中心进行缩放的
}
return;
}
}
void CustomGraphicsItem::setQGraphicsViewWH(int nwidth, int nheight)//将主界面的控件QGraphicsView的width和height传进本类中,并根据图像的长宽和控件的长宽的比例来使图片缩放到适合控件的大小
{
qDebug()<< nwidth<<nheight;
int nImgWidth = 1024;
int nImgHeight = 1040;
qreal temp1 = nwidth*1.0/nImgWidth;
qreal temp2 = nheight*1.0/nImgHeight;
if(temp1>temp2)
{
m_scaleDafault = temp2;
} else
{
m_scaleDafault = temp1;
}
// setScale(m_scaleDafault);
m_scaleValue = m_scaleDafault;
}
void CustomGraphicsItem::updatePix(QPixmap &pixmap)
{
m_hasPix=true;
m_pix = pixmap;
wid=m_pix.width();
hei=m_pix.height();
update();
}
void CustomGraphicsItem::ResetItemPos()//重置图片位置
{
m_scaleValue = m_scaleDafault;//缩放比例回到一开始的自适应比例
setPos(0,0);
// setScale(m_scaleDafault);//缩放到一开始的自适应大小 setPos(0,0);
}
qreal CustomGraphicsItem::getScaleValue() const
{
return m_scaleValue;
}
QGraphicsItem拖动+缩放+实时更新pixmap
最新推荐文章于 2024-04-09 17:54:57 发布