2

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.

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.

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 *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();

Health- and energy bars

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

jdepypere
  • 3,453
  • 6
  • 54
  • 84

1 Answers1

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 :

    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 :

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

You can also add it to an item :

item->setParentItem(anOtherItem);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Works fine, but is there also a way to really display it above the whole scene, or rather, display the scene under the widget and have the widget extend to the width of the screen? The scene I have positions items across the whole scene, and using this method I would have to offset them all by the height of the widget. – jdepypere Nov 29 '14 at 12:37
  • Do you want to display your widget inside the scene or you just want it to be above the view and out side of it? – Nejat Nov 29 '14 at 12:54
  • It does not have to display inside the scene, I want it to display above my scene. – jdepypere Nov 29 '14 at 13:38
  • @arbitter There are many ways to do it. You can use a layout and add both the widget and view in it. I have updated the answer to show one possible way. – Nejat Nov 29 '14 at 15:58