PDA

View Full Version : QGraphicsView - Can't set a scene



Early09
14th January 2011, 09:54
Hello all,

I am new in Qt programming.

I have a few problems with the QGraphicsView widget.

If I use the following code, a QGraphicsView window will open and show me "Hello".


#include <QtGui/QApplication>
#include <QtGui>

int main(int argc, char *argv[])
{
//scene.addPixmap(QPixmap('pic.jpg'));
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView grview;
scene.addText("Hello!");
grview.setScene(&scene);

grview.show();

return app.exec();

}

But if I will use the Designer and have following:


//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

public slots:
void slot1();
};

#endif // MAINWINDOW_H



//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::slot1()
{
QGraphicsScene scene;
scene.addText("Hello!");
ui->graphicsView->setScene(&scene);

ui->graphicsView->show();
}



//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>636</width>
<height>503</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>256</width>
<height>192</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>90</x>
<y>280</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>636</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>slot1()</slot>
<hints>
<hint type="sourcelabel">
<x>120</x>
<y>328</y>
</hint>
<hint type="destinationlabel">
<x>240</x>
<y>297</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>slot1()</slot>
</slots>
</ui>


Nothing will be appear on the QGraphicsView, which I placed with the QtCreator gui designer.

I don't know what I can do, to solve the problem. Thank you in advance for your help.

Best regards

Ps.: Windows 7 x64, Qt Creator 2.0.1 with Qt 4.7.0

szisziszilvi
14th January 2011, 10:28
try in this way:


QGraphicsScene *scene = new QGraphicsScene;
scene->addText("hello");
ui->graphicsView->setScene(scene);

with this code it works for me.

high_flyer
14th January 2011, 10:39
You have a scope problem.
Your scene is local to the slot, and gets destroyed at the end of it.
Make the scene a member of your class.


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
QGraphicsScene m_scene;
Ui::MainWindow *ui;

public slots:
void slot1();
};


void MainWindow::slot1()
{
// QGraphicsScene scene;
m_scene.addText("Hello!");
ui->graphicsView->setScene(&m_scene);

ui->graphicsView->show();
}

Early09
14th January 2011, 10:42
try in this way.......

Thank you it worked by me also. So could you can also explain me why, this way works or what the differences is?

szisziszilvi
14th January 2011, 10:46
Well, actually I have no idea...:o
As another beginner, I'm also interested in the answer.

high_flyer
14th January 2011, 10:52
So could you can also explain me why, this way works or what the differences is?
For the reason I said in my previous post.
When you allocate on the stack, the variable gets destroyed at the end of the scope (slot in this case).
When you allocate on the heap, it only gets destroyed when the process exits - and if you don't make sure to delete it, it will leak memory.
My solution still creates the scene on the stack, thus you don't have to worry about deleting it, but it will live as long as your object does.

P.S
This is not a Qt issue, but a basic C/C++ issue.
Try first to learn C/C++ without Qt, then learn Qt.

MarekR22
14th January 2011, 10:58
Thank you it worked by me also. So could you can also explain me why, this way works or what the differences is?
This quite simpel if you know basics of C++. In your slot you have created local object QGraphicsScene scene; which is automaticly destroyed when execution of this slot ends.
And since scene has ownership of text evry thing you have created in slot is imiedetly destoyed.
In other cases scene exists as long as MainWidow object exists.