首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在图形视图上移动qwidget?

如何在图形视图上移动qwidget?
EN

Stack Overflow用户
提问于 2018-06-20 10:06:15
回答 2查看 1.5K关注 0票数 2

QGraphicsView上,我设置了一个QGraphicsScene。我通过一个QDial小部件添加一个QGraphicsProxy对象。如何移动QDial对象?

代码语言:javascript
复制
    QDial *dial = new QDial;// dial object
    dial->setGeometry(event->pos().x(),event->pos().y(),80,80);// placing on mouse position
    QSizeGrip * sizeGrip = new QSizeGrip(dial);

    QHBoxLayout *layout = new QHBoxLayout(dial);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(sizeGrip, 0, Qt::AlignRight | Qt::AlignBottom);

    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
    proxy->setWidget(dial);
    proxy->setFlag(QGraphicsItem::ItemIsMovable,true);
    scene->addItem(proxy);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-03 06:06:54

在这段代码中,QGraphicsWidget是GraphicItem,通过使小部件的父级,您可以在scene.setflags上移动小部件。

代码语言:javascript
复制
   QDial *dial = new QDial;// dial object
   dial->setGeometry(event->pos().x(),event->pos().y(),80,80);// placing on mouse position
   QSizeGrip * sizeGrip = new QSizeGrip(dial);

   QHBoxLayout *layout = new QHBoxLayout(dial);
   layout->setContentsMargins(0, 0, 0, 0);
   layout->addWidget(sizeGrip, 0, Qt::AlignRight | Qt::AlignBottom);

   QGraphicsWidget* parentWidget = new QGraphicsWidget();//make parent of widget
   parentWidget->setCursor(Qt::SizeAllCursor);
   parentWidget->setGeometry(event->scenePos().x(),event->scenePos().y(),width.toInt(), height.toInt());
   parentWidget->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable );
   addItem(parentWidget);

   QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
   proxy->setWidget(dial);
   proxy->setParentItem(parentWidget);
票数 0
EN

Stack Overflow用户

发布于 2018-06-20 13:56:29

QDial放入QGraphicsProxyWidget只是第一步。

由于代理不支持移动,所以可以将其放入QGraphicsItem (例如rect)中,并使用它移动带有QDial的代理:

代码语言:javascript
复制
QDial *dial = new QDial();

QGraphicsRectItem* movableGraphicsItem = scene->addRect(event->pos().x(), event->pos().y(), 80, 80);
movableGraphicsItem->setFlag(QGraphicsItem::ItemIsMovable, true);
movableGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, true);

QGraphicsProxyWidget* proxy = scene->addWidget(dial);
proxy->setPos(event->pos().x(), event->pos().y() + movableGraphicsItem->rect().height());
proxy->setParentItem(movableGraphicsItem);

movableGraphicsItem->setRotation(180); // Test by rotating the graphics item

我还没有测试这一点,你可能需要发挥周围的大小,位置,布局和大小的抓地力,你正在使用,但这是基础,从哪里你可以开始。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50945590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档