December 21, 2011

webguy85 webguy85
Lab Rat
6 posts

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):

  1. widget = new QWidget();
  2. vlayout = new QVBoxLayout(widget);
  3. scene.addText("Hello, world!");
  4. QGraphicsView view(&scene);
  5. view.show();
  6.  
  7. //Here I want to place the view inside a layout object called vlayout

6 replies

December 21, 2011

Chris H Chris H
Lab Rat
139 posts

  1.  vlayout->addWidget(view);
Have a look at the documentation for QVBoxLayout [developer.qt.nokia.com] for more examples.

December 21, 2011

webguy85 webguy85
Lab Rat
6 posts

When I do that, I get an error:

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

December 21, 2011

broadpeak broadpeak
Hobby Entomologist
360 posts

You have to pass an address, so:

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

December 21, 2011

webguy85 webguy85
Lab Rat
6 posts

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?

December 22, 2011

webguy85 webguy85
Lab Rat
6 posts

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

  1. QGraphicsView view(&scene);
  2. QPixmap pixmap(":/images/icons/dsp.gif");
  3. QGraphicsPixmapItem* dsp = scene.addPixmap(pixmap);
  4. view.show();
  5. vlayout->addWidget(&view);
  6. 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?

December 22, 2011

Chris H Chris H
Lab Rat
139 posts

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.

 
  ‹‹ [Solved] Can anyone help-i miss something! filedownloader example      [SOLVED] QHostinfo: no such file or directory ››

You must log in to post a reply. Not a member yet? Register here!