How to place a GraphicsView in a layout object

  • I need to create a scene and place it in a GraphicsView. Once I finish the scene and place it in the GraphicsView, how do I place the GraphicsView inside a layout object? My layout object is called vlayout and is an instance of QVBoxLayout. This is what I have so far (mostly from online examples):

    widget = new QWidget();
    vlayout = new QVBoxLayout(widget);
    QGraphicsScene scene;
    scene.addText("Hello, world!");
    QGraphicsView view(&scene);;

    //Here I want to place the view inside a layout object called vlayout

  • @ vlayout->addWidget(view);@ Have a look at the "documentation for QVBoxLayout": for more examples.

  • When I do that, I get an error:

    'QBoxLayout::addWidget' : cannot convert parameter 1 from 'QGraphicsView' to 'QWidget *'

  • You have to pass an address, so:

    vlayout->addWidget(&view); // & = reference as address

  • I see. Im new to C++ so I am just getting used to pointers and addresses. Thanks.

    I dont see the text though. Any idea why?

  • Syntactically, everything is fine, but I can't get my image to show up. This is what I have:

    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QPixmap pixmap(":/images/icons/dsp.gif");
    QGraphicsPixmapItem* dsp = scene.addPixmap(pixmap);;
    vlayout->addWidget(new QPushButton("some button here"));

    The QPushButton shows up fine in the layout, but not the view. Any idea what I am doing wrong?

  • Assuming this is in a function, the QGraphicsView is a local variable: when it goes out of scope it's gone. You should dynamically allocate it (this seems to be "the Qt way") or make it a member variable of your class.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.