怎么在qgraphics中添加控件
时间: 2024-02-21 14:59:17 浏览: 44
在 Qt 的 QGraphicsView 中添加控件的一种常见方法是使用 QGraphicsProxyWidget。具体步骤如下:
1. 创建一个 QWidget 控件,例如一个 QPushButton。
2. 创建一个 QGraphicsProxyWidget 对象,将该 QWidget 控件作为参数传递给它。
3. 将该 QGraphicsProxyWidget 对象添加到 QGraphicsScene 中。
4. 将 QGraphicsView 的场景设置为该 QGraphicsScene 对象。
以下是一个简单的示例代码,演示了如何在 QGraphicsView 中添加一个 QPushButton:
```
QPushButton *button = new QPushButton("Click me!");
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(button);
QGraphicsScene *scene = new QGraphicsScene();
scene->addItem(proxy);
QGraphicsView *view = new QGraphicsView();
view->setScene(scene);
view->show();
```
注意,此示例仅适用于在 Qt 的 QGraphicsView 中添加单个控件。如果需要添加多个控件,则需要在 QGraphicsScene 中创建多个 QGraphicsProxyWidget 对象,并将它们都添加到场景中。