Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ui.qml text object needs to be edited from another .qml file



  • I have read a lot on this topic and for some reason can't find one that works (may be user error). I have a program that displays a text box that was created in the .ui.qml file. I want to be able to edit the text from a .qml file and have the changes appear in the .ui.qml file. the ui file is nice for designing with the design mode. However, it does not allow for function calls. So i need to be able to change the text from a .qml file.

    //Display.qml
    
    DisplayFom {
    
    propety alias custom: custom.text
    
    Text {
    id: custom
    text: "Hello World"
    }
    
    
    //DisplayForm.ui.qml
    
    Item {
        id: element
        x: 0
        width: 800
        height: 800
    
        Text {
            id: title
            text: custom
        }
    }
    


  • hi @texasRanger
    the propety alias sould be in your DisplayForm.ui.qml file

    //DisplayForm.ui.qml

    Item {
        width: 400
        height: 400
    
        property alias textItem: element //<
    
        Text {
            id: element
            x: 188
            y: 193
            text: qsTr("Text")
            font.pixelSize: 12
        }
    }
    
    

    //Display.qml

    DisplayForm {
        textItem.text: "Hello World"
    }
    

    then you can create Display components in other qml files

    example main.qml

    Window {
        id:root
        visible: true
        width: 640
        height: 480
        Display{
            textItem.text: "hi"
        }
    }
    


  • @LeLev I appreciate your response. Sorry, I'm very new to Qt and qml. So without the Display component in main.qml and if we only had display and display form. Would Display.qml be able to change the text to "Hello World" with the example you gave?



  • @texasRanger yes, you can define the text inside Display.qml like i did in the example .you can test it.



  • @LeLev I must be missing something else (whether its a piece of code or knowledge), cause mine still displays the word "Text". All i have are the two files, DisplayForm.ui.qml and Display.qml, and these files are not a part of any project (Did not know if that mattered). Since they are not a part of any project I can't actually run or build these files. So when i make a change in the ui.qml file it updates in real time. Is it supposed to be the same for the .qml file when it makes an edit to the .ui.qml file?



  • @texasRanger said in ui.qml text object needs to be edited from another .qml file:

    he ui.qml file it updates in real time

    how do you show it on the screen ? with qmlscene tool ?

    if you set textItem.text : "Hello World" inside Display.qml and show it with qmlscene you will see "Hello World".
    but if you show DisplayForm.ui.qml you will still see "Text"