[solved] text inside custom node in the scene graph



  • 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 ...



  • 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.



  • 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 ...



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.