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);
view.show();//Here I want to place the view inside a layout object called vlayout
@ -
@ vlayout->addWidget(view);@ Have a look at the "documentation for QVBoxLayout":http://developer.qt.nokia.com/doc/qt-4.8/QVBoxLayout.html 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);
view.show();
vlayout->addWidget(&view);
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.