QGraphicsItem的缩放
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了QGraphicsItem的缩放,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2026字,纯文字阅读大概需要3分钟。
内容图文
QGraphicsItem的缩放
QgarphicsItem是Qt视图体系中的项。QGraphicsItem本身是不支持鼠标拖动来缩放的,本文介绍如何通过更改鼠标事件来修改项的大小。(本文所用Qt版本为Qt4.8)
下文代码实现的功能为:按住shift,再用鼠标拖动,可以改变Box的大小。
定义类Box
class Box:public QGraphicsItem { Q_DECLARE_TR_FUNCTIONS(Box)public: Box(); ...protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); };
Box::Box() { setFlags(QGraphicsItem::ItemIsSelectable| QGraphicsItem::ItemIsMovable| QGraphicsItem::ItemSendsGeometryChanges| QGraphicsItem::ItemIsFocusable); //接受键盘事件 mBoundingRect = QRectF(0,0,100,100); mBoundingRect.translate(-mBoundingRect.center()); }
上面两段代码为Box类的定义及构造函数的实现,最重要的是三个鼠标函数的重载,及在setFlag中使Box可以接受键盘事件。
重载mousePressEvent
void Box::mousePressEvent(QGraphicsSceneMouseEvent *event) { if(event->modifiers()&Qt::ShiftModifier) { resizing = true; //resizing变量在鼠标点击时变为true //在放开时变为false setCursor(Qt::SizeAllCursor);//鼠标样式变为十字 } else QGraphicsItem::mousePressEvent(event); }
重载mouseMoveEvent
void Box::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if(resizing) { QRectF rect(mBoundingRect); if(event->pos().x()<rect.x()) rect.setBottomLeft(event->pos()); else rect.setBottomRight(event->pos()); mBoundingRect=rect; mBoundingRect.translate(-mBoundingRect.center()); scene()->update(); } else QGraphicsItem::mouseMoveEvent(event); }
在这里,简单的更新Box的左下角和右上角来匹配鼠标位置。更好的做法是分别处理x和y坐标。
重载mouseReleaseEvent
void Box::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if(resizing) { resizing = false; setCursor(Qt::ArrowCursor); } else QGraphicsItem::mouseReleaseEvent(event); }
用户在改变大小的过程中放开鼠标,就将resizing改为true,以及将鼠标样式变回箭头。
以上就是QGraphicsItem的缩放的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的QGraphicsItem的缩放全部内容,希望文章能够帮你解决QGraphicsItem的缩放所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。