QT在QGraphicsScene上方显示QWidget

时间:2023-02-02 00:15:29

Currently I have a QGraphicsScene that is put inside a QGraphicsView and is shown on the display. I add all my elements to my scene that I set as the active scene.

目前我有一个放在QGraphicsView中的QGraphicsScene并显示在显示器上。我将所有元素添加到我设置为活动场景的场景中。

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView w;
    GameScene *gameScene = new GameScene(); // GameScene extends QGraphicsScene, adds tons of elements to the scene
    w.setScene(gameScene);
    w.show();
    return a.exec();
}

Above this scene I want a bar that contains several layout elements, like several QProgressBar.

在这个场景之上,我想要一个包含几个布局元素的栏,比如几个QProgressBar。

For what I have found so far, QWidget's can be positioned easily. I've made already a widget of what I need to be displayed above the scene:

对于我到目前为止所发现的,QWidget可以轻松定位。我已经制作了一个我需要在场景上方显示的小部件:

QWidget *dummyWidget = new QWidget();
QFormLayout *formLayout = new QFormLayout;
QProgressBar *bar1 = new QProgressBar();
QProgressBar *bar2 = new QProgressBar();
bar1->setValue(20);
bar2->setValue(100);
formLayout->addRow("&Health:", bar1);
formLayout->addRow("&Energy:", bar2);
dummyWidget->setLayout(formLayout);
dummyWidget->show();

QT在QGraphicsScene上方显示QWidget

But how do I get this to be displayed above my QGraphicsScene?

但是如何让它显示在我的QGraphicsScene上方?

1 个解决方案

#1


1  

If you want to display your widget above the view you can have a layout similar to the one for dummyWidget and add the widget and view in it :

如果要在视图上方显示窗口小部件,可以使用类似于dummyWidget的布局,并在其中添加窗口小部件和视图:

    QGraphicsView w;

    QWidget *widget = new QWidget();
    QFormLayout *formLayout2 = new QFormLayout(widget);

    QWidget *dummyWidget = new QWidget();
    QFormLayout *formLayout = new QFormLayout;
    QProgressBar *bar1 = new QProgressBar();
    QProgressBar *bar2 = new QProgressBar();
    bar1->setValue(20);
    bar2->setValue(100);
    formLayout->addRow("&Health:", bar1);
    formLayout->addRow("&Energy:", bar2);
    dummyWidget->setLayout(formLayout);

    formLayout2->addRow("", dynamic_cast<QWidget*>(dummyWidget));
    formLayout2->addRow("", dynamic_cast<QWidget*>(&w));

    widget->show();

If you want to add the widget in the scene, you can use QGraphicsScene::addWidget which creates a new QGraphicsProxyWidget for widget, adds it to the scene, and returns a pointer to the proxy :

如果要在场景中添加窗口小部件,可以使用QGraphicsScene :: addWidget为窗口小部件创建新的QGraphicsProxyWidget,将其添加到场景中,并返回指向代理的指针:

QGraphicsProxyWidget * item = gameScene->addWidget(dummyWidget);
item->setPos(100,100);
item->setZValue(1);

You can also add it to an item :

您还可以将其添加到项目中:

item->setParentItem(anOtherItem);

#1


1  

If you want to display your widget above the view you can have a layout similar to the one for dummyWidget and add the widget and view in it :

如果要在视图上方显示窗口小部件,可以使用类似于dummyWidget的布局,并在其中添加窗口小部件和视图:

    QGraphicsView w;

    QWidget *widget = new QWidget();
    QFormLayout *formLayout2 = new QFormLayout(widget);

    QWidget *dummyWidget = new QWidget();
    QFormLayout *formLayout = new QFormLayout;
    QProgressBar *bar1 = new QProgressBar();
    QProgressBar *bar2 = new QProgressBar();
    bar1->setValue(20);
    bar2->setValue(100);
    formLayout->addRow("&Health:", bar1);
    formLayout->addRow("&Energy:", bar2);
    dummyWidget->setLayout(formLayout);

    formLayout2->addRow("", dynamic_cast<QWidget*>(dummyWidget));
    formLayout2->addRow("", dynamic_cast<QWidget*>(&w));

    widget->show();

If you want to add the widget in the scene, you can use QGraphicsScene::addWidget which creates a new QGraphicsProxyWidget for widget, adds it to the scene, and returns a pointer to the proxy :

如果要在场景中添加窗口小部件,可以使用QGraphicsScene :: addWidget为窗口小部件创建新的QGraphicsProxyWidget,将其添加到场景中,并返回指向代理的指针:

QGraphicsProxyWidget * item = gameScene->addWidget(dummyWidget);
item->setPos(100,100);
item->setZValue(1);

You can also add it to an item :

您还可以将其添加到项目中:

item->setParentItem(anOtherItem);