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

[SOLVED]Accessing TabView component (accessing children properties of component)



  • Dear people,

    I have a qml application which has a TabView. Obviously I have created components to assign them to Tabs. There is a component that consists of 2 textfields. Outside of tabview I have a button. onClick of thay button I need to call a c++ function and pass texts of those 2 textfields. Using textfields' ids don't work, because they are created dynamically. How do I access, for example, textfield's text, which is inside a component assigned to a tabview?

    Optionally, here is brief version of my code:

    @ TabView {
    x: 8
    y: 8

        id:tab
    
        Component.onCompleted: {
    
            addTab("Type", typeComoponent);
            addTab("Unit", unitTab)
    
        }
    
    
    }
    

    @

    One of components:
    @ Component {

        id:typeComoponent
    
        GroupBox{
    
            id: groupbox1
            width: 63.2
    
    
            TextField {
    
                id: typefield
    
            }
    
    
            TextField {
    
                id: baseField
    
            }
    
        }
    
    }
    

    @

    I want to access those from here:
    @
    Button {

        id: saveBtn
        
        onClicked: {
    
            adddial.typeSaveClicked(...); //Have to pass tests of tesfields to this function
    
        }@


  • This is kind of important thing, why isn't anybody answering ?



  • There are several ways to do this based on scoping rules. One solution is for instance to share root properties that your text fields update when modified or accepted.

    @import QtQuick.Controls 1.1
    import QtQuick 2.1

    Item {
    width: 500
    height: 500
    property string typeResult
    Label {
    y: 200
    id: saveBtn
    text: "Text field is now " + typeResult
    }
    TabView {
    Component.onCompleted: { addTab("Type", typeComoponent); }
    Component {
    id: typeComoponent
    GroupBox{ TextField { anchors.centerIn: parent ; onTextChanged: typeResult = text} }
    }
    }
    }
    @



  • I have solved my problem few minutes ago.

    @ Component {

        id:typeComoponent
    
        GroupBox{
    
            id: groupbox1
            
            property alias typeValue: typefield.text
            property alias unitValue: baseField.text
    
        
            width: 63.2
    
    
            TextField {
                 id: typefield
             }
      
             TextField {
                 id: baseField
             }
        }
     }@
    

    and then:
    @

                adddial.typeSaveClicked(tab.getTab(0).item.typeValue)
    

    @

    In general, I didn't how to access TabView's component (getTab(0).item)



  • Just note that you should verify that the "item" property is not null, as tabs get instantiated when they are first shown and not at the time that they are added.



  • Yes, thanks. I did verify. I just felt it is not important in this case (my question was different) :)



  • With getTab(0) you get first tab? What about active tab, how to get it?



  • [quote author="Lepa Brena" date="1389088342"]With getTab(0) you get first tab? What about active tab, how to get it?[/quote]

    Since every tab has different components, you can't access the active tab directly (if there was a way, there wouldn't be any use in any ways). You can use "currentindex":http://qt-project.org/doc/qt-5/qml-qtquick-controls-tabview.html#currentIndex-prop variable to get the index of tab and then check which tab it is(to be able to use components) and access your tab with gettab().



  • Foolish me.
    Thank you khajvah.


Log in to reply