Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Displaying a text in a QGraphicsScene
QtWS25 Last Chance

Displaying a text in a QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sayanel
    wrote on last edited by
    #1

    This is a newbie 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 :

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

    MyClass foo;
    ui->graphicsView->setScene(foo.scene);
    

    with MyClass.h having

    QGraphicsScene *scene;
    

    and MyClass.cpp, in the constructor of the class :

    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

    P.S.: Where shall I post newbie question ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,
      @Sayanel said in Displaying a text in a QGraphicsScene:

      MyClass foo;
      ui->graphicsView->setScene(foo.scene);

      What it is the lifetime of foo ?

      If it's declared in the same method where you make the ui call, then it will be destroyed before anything else happens.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • S Offline
        S Offline
        Sayanel
        wrote on last edited by Sayanel
        #3

        If think that is is, foo is deleted at the end of the mainwindow constructor. Thank you sir ! I just have to do in the mainwindow.cpp:

            MyClass *foo = new MyClass;
            ui->graphicsView->setScene(foo->scene);
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Except that now you are leaking memory as this object pointer gets lost.

          You should make it a member of your MainWindow class and handle it's destruction properly. Or use e.g QScopedPointer to store it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sayanel
            wrote on last edited by
            #5

            What if I just declare foo as a child of mainwindow

            MyClass *foo = new MyClass(this);
            

            it will be destructed when I close my mainwindow ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              If MyClass is a QObject based class. But if you need that kind of stuff for a pretty simple task the way you want to do it. you might want to reconsider its design.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sayanel
                wrote on last edited by
                #7

                Well MyClass inherits from QWidget
                The idea I had was to have MyClass handle everything inside my 'graphicsView' -in order to not have it pollute the main windows-. This way I can interact with the scene using MyClass method and I can have it have slots.
                The idea is to crate a zone (foo.scene, displayed on the widget graphicsView) where I will be able to put all the object I want at the position I want.

                I found this example (https://doc.qt.io/qt-5/qtwidgets-graphicsview-chip-example.html) which I think does what I want (via its class 'View')

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved