Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved] text inside custom node in the scene graph
Forum Updated to NodeBB v4.3 + New Features

[solved] text inside custom node in the scene graph

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 3.1k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    galatea
    wrote on 18 Oct 2013, 13:32 last edited by
    #1

    I just recently started using Qt, so this may very well be an easy question.

    I created a subclass of QQuickItem to display a custom design diagram that can be used as a node in a QML based design hirarchy. The diagram class does the rendering of the actual diagram in its implementation of updatePaintNode (using a QSGGeometryNode), which works just fine. But now a need to add text labels as a scale. This is supposed to be done by the diagram object itself, so I can not simply add a 'Text' node in a QML file, since right now there is no such file. I tried searching for the backend class in order to instantiate it dynamically and programatically create the necessary subtree of my diagram object, but the class in question (QQuickText) seems to be part of the internal system of QtQuick and therefore is not supposed to be instantiated that way.

    Now for the actual question: Am I missing a default class that can be used as a programatically creaded subtree node rendering plain text to the screen? Or is there any straight forward way to do this. The only method I could think of, that might achive something like this, would be to subclass QQuickPaintedItem and create the node myself, but since the introduction specifically states the advantages of the scene graph over the QPainter approach that feels like going around the actual method of choice somehow ...

    1 Reply Last reply
    0
    • U Offline
      U Offline
      unai_i
      wrote on 21 Oct 2013, 09:57 last edited by
      #2

      Hi,
      I faced the same problem and finally added text to my custom item by instantiating QML Text elements via QQmlComponent objects and managing them in updatePolish() method. Then I call polish whenever I need to update those texts.
      Hope this helps you.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        galatea
        wrote on 21 Oct 2013, 14:53 last edited by
        #3

        Thanks,
        I figured that I had to instantiate Text elements dynamically. But I had not found any way to do that. If I understand you correctly, I need to create a qml-file containing a single text element, instantiate it at runtime and integrate the new element into my tree.
        Instantiating the element worked, but sofar I failed to get the new element to actually show up on screen. And since the QQuickText type is not exposed by the framework I have yet to figure out a way, how to pass the actual content to the element.

        This is what my latest attempt looks like (located in the constructor of the custom diagram node). Maybe you can give me a little more detailed hint on how to get this to work.
        @
        QQmlEngine* e=new QQmlEngine();
        QQmlComponent* c=new QQmlComponent(e,"some path");
        QQuickItem* item= qobject_cast<QQuickItem*>(c->create());
        item->setProperty("text","Hello World!");
        item->setParent(this);
        item->setVisible(true);
        @
        Although I still have a hard time believing, there is no more direct approach to this problem than using an external helper qml file ...

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unai_i
          wrote on 21 Oct 2013, 17:58 last edited by
          #4

          Hi,
          I will check if there are more steps in my code when I have access to it but one thing your code is missing is:
          @
          item->setParentItem(this);
          @

          which is the line used to insert the element into the scene.
          Furthermore you don't especially need a qml file for the text element as you can input qml code into the QQmlComponent through the setData method which can take a text string.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            galatea
            wrote on 22 Oct 2013, 08:02 last edited by
            #5

            Thank you very much,
            obviously I confused setParent and setParentItem. Now the code finally does what it is supposed to do. I still can not believe this really is the way you are supposed to do this, but it works, so I guess my problem is solved for now.

            1 Reply Last reply
            0

            1/5

            18 Oct 2013, 13:32

            • Login

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