Adding QgraphicsScene to my QgraphicsView
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
Re: Adding QgraphicsScene to my QgraphicsView
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?
Re: Adding QgraphicsScene to my QgraphicsView
"&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.
Re: Adding QgraphicsScene to my QgraphicsView
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,
Code:
#include <QtGui>
#include <QPrinter>
#include <QGraphicsScene>
#include <QStyleOption>
#include <math.h>
#include "myqtapp.h"
#include "mouse.h"
static const int MouseCount = 2;
{
setupUi(this);
connect( pushButton_newgame, SIGNAL( clicked() ), this, SLOT( newgame() ) );
scene.setSceneRect(-100, -100, 200, 200);
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();
}
Re: Adding QgraphicsScene to my QgraphicsView
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.
Re: Adding QgraphicsScene to my QgraphicsView
Thank you, found my issues and fixed them
Chris