Displaying a text in a QGraphicsScene

I am trying to create a scene with Qt to display what I want (lets say for now just a text), but with the code describing the content being in another class.

I have a mainwindows, created with QtCreator gui, which contains the widget "graphicView"
In my mainwindows.cpp, I can do :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <QGraphicsScene>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QGraphicsScene *scene = new QGraphicsScene();
    scene->addText("Hi");
    ui->graphicsView->setScene(scene);
}

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


And it will display the text ('Hi!'). Now what I want to have is a class MyClass in charge of what is inside the widget graphicsView, and do in mainwindows.cpp:
1
2
MyClass foo
ui->graphicsView->setScene(foo.scene);


with MyClass.h having
 
QGraphicsScene *scene;


and MyClass.cpp, in the constructor of the class :
1
2
scene = new QGraphicsScene(this);
scene->addtext("Hi again!");


But when running it it doesn't work, it doesn't display anything.
If I add this at the end of the mainwindows.cpp
 
std::cout << foo.scene->items().size() << std::endl;

I can see that my scene do contains one element, but for some reason it isn't displayed. Any hint why ?
Thanks
MainWindow.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP


#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QGraphicsView>
#include <QMainWindow>
#include <QString>


namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    QGraphicsTextItem* setQGSInitialText(const QString& val);

    QGraphicsScene* getScene() const;
    QGraphicsView* getView() const;

private:
    Ui::MainWindow *ui;
    QGraphicsScene* scene { nullptr };
    QGraphicsView* view { nullptr };
};


#endif // MAINWINDOW_HPP 


MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "MainWindow.hpp"
#include "ui_MainWindow.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <memory>


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

    scene = new QGraphicsScene( this );

    view = new QGraphicsView( scene, this);
    view->show();

    setCentralWidget( view );
}


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


QGraphicsTextItem* MainWindow::setQGSInitialText(const QString& val)
{
    return scene->addText(val);
}


QGraphicsScene* MainWindow::getScene() const
{
    return scene;
}

QGraphicsView* MainWindow::getView() const
{
    return view;
}


MyClass.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef MYCLASS_HPP
#define MYCLASS_HPP


#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QGraphicsView>
#include <QString>


class MyClass
{
public:
    MyClass(QGraphicsScene* scene_arg, QGraphicsView* view_arg);
    void changeQGSText(QGraphicsTextItem* text_item, const QString& val);

private:
    QGraphicsScene* scene { nullptr };
    QGraphicsView* view { nullptr };
};

#endif // MYCLASS_HPP 


MyClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "MyClass.hpp"


MyClass::MyClass(QGraphicsScene* scene_arg, QGraphicsView* view_arg)
    : scene { scene_arg }
    , view { view_arg }
{
}


void MyClass::changeQGSText(QGraphicsTextItem* text_item, const QString& val)
{
    text_item->setPlainText(val);
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "MainWindow.hpp"
#include "MyClass.hpp"
#include <QApplication>
#include <QGraphicsTextItem>


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

    QGraphicsTextItem* text_item { mw.setQGSInitialText( "Hi" ) };

    mw.show();

    MyClass mc(mw.getScene(), mw.getView());
    mc.changeQGSText(text_item, "New text");

    return a.exec();
}


(There’s also MainWindow.ui, of course)
Well thank you for your help, from which I learn that this is way much harder than I though. I'll try to understand all of that
It doesn’t need to be complicated, but:

- QMainWindow might contain several widget; you can set the main one by setCentralWidget().

- In the Qt libraries, ‘parent’ objects take care of destroying the contained objects (please, note this is not related to the C++ parent-child inheritance, but to ownership!).
So, to avoid memory leaks, you’d better always give ‘parents’ to your objects (methods like setCentralWidget() do that by default, anyway).

- When you use a QGraphicsScene, you usually need a QGraphicsView.

- If you want your QGraphicsScene to be owned by QMainWindow, but ruled by another class, perhaps the simplest method is:
- define the QGraphicsScene inside the QMainWindow
- take ownership by setCentralWidget()
- then pass a pointer to it to the other class.

Happy coding!
Topic archived. No new replies allowed.