PDA

View Full Version : Adding QgraphicsScene to my QgraphicsView



procompmx5
12th May 2011, 17:55
Hello everyone,

I have a problem. I'm trying to add a QgraphicsScene to my QgraphicsView (that I adding in QT designer) If I construct a QgraphicsView from code it works fine (It will create a new window though and I don't need that.)

My QgraphicsView that I make in Designer is called game_view i'm trying to add the view as such... game_view->setScene(&scene)

When I do this I don't have any errors but nothing is appearing in the view!

Any help is good help

Thank you
Chris

meazza
12th May 2011, 22:24
Well do you have any items in your scene that should show up? Otherwise the scene it self is just blank white. Also how do you create the scene?

Lykurg
12th May 2011, 22:49
"&scene" leads only to one conclusion (most likely): You created the scene on the stack, thus it is getting destroyed at the end of the function. Allocate the scene on the heap or as a member variable of the class which contains the view.

procompmx5
13th May 2011, 01:42
Yes the scene is populated...

I'm a bit confused by what you mean this is part of my code...

I borrowed some code from the QT examples for this,


#include <QtGui>
#include <QPrinter>
#include <QGraphicsScene>
#include <QStyleOption>
#include <math.h>
#include "myqtapp.h"

#include "mouse.h"

static const int MouseCount = 2;

myQtApp::myQtApp(QWidget*)
{
setupUi(this);
connect( pushButton_newgame, SIGNAL( clicked() ), this, SLOT( newgame() ) );
QGraphicsScene scene;
scene.setSceneRect(-100, -100, 200, 200);
scene.setItemIndexMethod(QGraphicsScene::NoIndex);

for (int i = 0; i < MouseCount; ++i) {
Mouse *mouse = new Mouse;
mouse->setPos(::sin((i * 6.28) / MouseCount) * 200,
::cos((i * 6.28) / MouseCount) * 200);
scene.addItem(mouse);
}


//QGraphicsView view(&scene);
game_view->setScene(&scene);
//game_view->setRenderHint(QPainter::Antialiasing);
//game_view->setBackgroundBrush(QPixmap(":/images/cheese.jpg"));
//game_view->setCacheMode(QGraphicsView::CacheBackground);
//game_view->setDragMode(QGraphicsView::ScrollHandDrag);
//game_view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
//game_view->resize(400, 300);
//game_view->show();
}

Lykurg
13th May 2011, 06:29
Yeah, it is exactly like I said. Your scene is deleted at the end of the constructor. So you can not expect to see something that already gets deleted. It's all about basic C++ knowledge: Live time of objects. Feel free to contact any c++ reference. And please use the code tags next time.

procompmx5
13th May 2011, 17:05
Thank you, found my issues and fixed them
Chris