Qt将Widget添加到GraphicsView?

问题描述:

有没有办法在QGraphicsView而不是QGraphicsScene上绘制小部件,以便在场景移动时小部件保持位置?Qt将Widget添加到GraphicsView?

我想创造一些对话是这样的工作空间内可停靠: http://www.thebandfrom.com/wp-content/uploads/photoshop-ui.png

可以使用addWidget功能QGraphicsScene,然后设置QGraphicsItem::ItemIgnoresTransformations标志添加QGraphicsProxyWidget

QGraphicsProxyWidget* proxyWidget = scene->addWidget(myWidget); 
proxyWidget->setFlag(QGraphicsItem::ItemIgnoresTransformations); 

您可以直接通过设置QGraphicsView其父的小部件添加到QGraphicsView。您还可以添加布局,以便在调整QGraphicsView的大小时,您的小部件会自行适当地进行排列。