properties of a component loaded by TabView



  • i have the issue where the height value is 0 if i read it right after the loading.
    here is my code

    void foo(){
    do stuf...
    
    finishCreation();
    
    do stuf....
    }
    
    Component{
                   id: someId
                   Rectangle{
                       anchors.fill:parent
                       color:"red"
    
                   property int val1:2
                   property int val2:3
                   function getValues(){
                       return val1+val2
                   }
                   }
               }
    Timer{
                id : myTimer
                running: false
                repeat: false
                interval: 1//milli seconde
                onTriggered: {
                        console.log("from timer",mainTabView.getTab(4).item.height);
                }
            }
        function finishCreation(){
            var tabComponant = Qt.createComponent("qrc:/gui/qmlui/qml.qml");
            var tab = mainTabView.addTab("main",someId)
            tab.loaded.connect(fullyLoaded)
            tab.active = true;
           // tab.item.update() // useless
          // mainTabView.update() // useless
           console.log("calling the function ",mainTabView.getTab(4).item.getValues()) // this call return the right values of properties
            console.log("right after creation",mainTabView.getTab(4).item.height);// index 4, represent this added tab
            myTimer.running = true;// activate the timer
        }
    function fullyLoaded(){ // here we are sure that every thing is loaded and still print 0
            console.log("fully loaded",mainTabView.getTab(4).item.height);
        }
    

    the result of console.log() in order of execution:
    fully loaded 0
    calling the function 5 // right ;)
    right after creation 0
    from timer 512

    my guessing is that the timer is executed by the event loop, and at that point every thing is ready.
    my question, is how to access the component properties(height width) right after creating them and/or adding them to the TabView. (using a timer is a poor implementation)


  • Moderators

    Hi @redouane and Welcome,

    AFAIK that operation is asynchronous since Tab inherits Loader. So by the time you print the height the tab is not yet loaded.
    Try loading a a Tab which has very minimal content to verify the above scenario. You might get the expected height.



  • @p3c0 said:

    asynchronous

    the documentation said that by default it is synchronous.
    i did modify the code as you said and i did add a function to call (see the new code), calling the function return the right values, so i assume its loaded.


  • Moderators

    @redouane Sorry my bad.
    I tried to recreate the same problem with this minimal example. It works as expected.

    import QtQuick 2.5
    import QtQuick.Controls 1.3
    
    Rectangle {
        width: 400
        height: 400
    
        Component{
            id: someId
            Rectangle{
                anchors.fill:parent
                color: '#'+Math.floor(Math.random()*16777215).toString(16);
            }
        }
    
        TabView {
            id : mytab
            anchors.fill: parent
        }
    
        Button {
            id: btn
            anchors.bottom: parent.bottom
            text: "Add new tab"
            onClicked: {
                var tab = mytab.addTab(mytab.count+1,someId)
                mytab.currentIndex = mytab.count-1
                
                console.log(mytab.getTab(mytab.count-1).item.height)
            }
        }
    }
    

    Can you try it ?

    Also are you sure you are referring the proper index ? i.e 4
    mainTabView.getTab(4).item.height



  • yes, if you look in my example, when i call the function getValues() inside the component it return the right value, which means that it is loaded and the index is right.


Log in to reply
 

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