在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);发布于 2018-08-03 06:06:54
在这段代码中,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);发布于 2018-06-20 13:56:29
将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我还没有测试这一点,你可能需要发挥周围的大小,位置,布局和大小的抓地力,你正在使用,但这是基础,从哪里你可以开始。
https://stackoverflow.com/questions/50945590
复制相似问题