2

On a QGraphicsView I set a QGraphicsScene. I add a QDial object through a QGraphicsProxy widget. How to move the QDial object?

    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);
Exa
  • 4,020
  • 7
  • 43
  • 60
V. Purbia
  • 57
  • 5

2 Answers2

1

Putting the QDial into a QGraphicsProxyWidget is only the first step.

Since the proxy does not support moving, you can put it into a QGraphicsItem (e.g. a rect) and use this to move the proxy with the QDial in it:

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

I have not tested this, you may have to play around with the sizing, the position and the layout and size grip you are using, but this is the base from where you can start.

Exa
  • 4,020
  • 7
  • 43
  • 60
  • thanks ... i am resizing rect item...i thought rect item is parent so that dial will automatic resize but its not happening. – V. Purbia Jun 21 '18 at 04:34
1

In this code QGraphicsWidget is GraphicItem by making parent of widget you can move widget on scene.setflags movable.

   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);
Sagar A W
  • 338
  • 1
  • 12