如何让QGraphicsView里的item绕中心点放大?

southbirdfly 2011-03-30 03:48:51
我调用QGraphicsItem的scale函数,但一直是绕左上角向右下角放大...
如何才能绕中心点向四周放大??
我试过QGraphicsView的centerOn函数和QGraphicsItem的setTransformOriginPoint函数都没有效果...
...全文
564 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
southbirdfly 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lijun_ay 的回复:]

我的代码可以啊,如下
void MainWindow::sceneScaleChanged(const QString &scale)
{
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
QMatrix oldMatrix = view->matrix();
……
[/Quote]

你这个是放大view吧? 我想放大的是view里的item...
zhh_kv 2011-04-02
  • 打赏
  • 举报
回复

1. 帮助文档中,提到了如果更改transform origin point,要设置setTransformOriginPoint函数的;
void QGraphicsItem::setScale ( qreal factor )
Sets the scale factor of the item. The default scale factor is 1.0 (i.e., the item is not scaled). A scale factor of 0.0 will collapse the item to a single point. If you provide a negative scale factor, the item will be flipped and mirrored (i.e., rotated 180 degrees).

The item is scaled around its transform origin point, which by default is (0, 0). You can select a different transformation origin by calling setTransformOriginPoint().

2. 在item scale之前,setTransformOriginPoint(中心点);然后再scale

上面你也提到了这样做,但没达到想要的效果,可能原因:
函数位置不正确 或者 函数参数不正确 等等;
davey_yy 2011-03-31
  • 打赏
  • 举报
回复
放大之后重新设置位置
lijun_ay 2011-03-31
  • 打赏
  • 举报
回复
我的代码可以啊,如下
void MainWindow::sceneScaleChanged(const QString &scale)
{
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
QMatrix oldMatrix = view->matrix();
view->resetMatrix();
view->translate(oldMatrix.dx(), oldMatrix.dy());
view->scale(newScale, newScale);
if (scene->selectedItems().count() > 0 )
{
QGraphicsItem * Item = scene->selectedItems().first();
//view->centerOn(Item); //以第一个选中的图形为中心
QPointF sceneP=Item->mapToScene(0,0);
view->centerOn(sceneP); //以第一个选中的图形位置为中心
}
}

16,822

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧