如何在Graphics View上移动qwidget?

时间:2018-06-20 10:06:16

标签: qt qgraphicsscene

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

    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);

2 个答案:

答案 0 :(得分:0)

QDial放入QGraphicsProxyWidget中只是第一步。

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

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

我尚未对此进行测试,您可能需要尝试调整大小,位置以及所使用的布局和大小,但是这是您可以开始使用的基础。

答案 1 :(得分:0)

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

   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);
相关问题