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

Dialog QML open passing variable



  • Hi. I made a dialog about QML that works, but I have some small problems. It must be outside the listView, but I must show text from the ListView model in the Dialog. In the onClicked of the MouseArea from which I open the dialog if I write this text in console.log that I want to display, I display it, so how can I pass this information to the dialog that I want to open to display it in there?
    Thanks



  • Hi Marco,

    I am a beginner but from what I found it is not possible to pass on a variable. What I did in your case is to use a "global" variable. With the button click I write the index of the clicked element to a variable that is accessible to the dialog. In the dialog I read in the element on the visible changed.

    ListView {
      id: dataView
      property int activeIndex: 0
    
      delegate: Rectangle {
        [...]
        Button {
           onClicked: activeIndex = index    
        }
      }
    }
    
    Dialog {
      onVisibleChanged: {
         var element = dataView.model.get(dataView.activeIndex)
      }
    }
    

    Hope that still helps =)



  • @D_Tec Couldn't you introduce a property in the Dialog and assign the required value to it before you open the dialog?



  • hi,

    @D_Tec said in Dialog QML open passing variable:

    var element = dataView.model.get(dataView.activeIndex)

    this will probably not work, activeIndex is just a property of the listview, it is not part of the model
    you should access it using the list id

     var element = dataView.activeIndex
    

    you can access to the model directly using get() method

        Component.onCompleted: {dataView.model.insert(0, {"age" : 100})}
        ListView {
            id: dataView
            model: ListModel{}
        }
      // dataView.model.get(0).age
    
    


  • This code works for me. My dialogs contain the information from the model that I want when they are opened. I do not see where the difference is to @LeLev. Where you put your information on which item the user clicked does not matter.

    @Bob64 I do not know how access your dialogs but I guess you can assign a property and access it with dialogId.propertyName.



  • @D_Tec "I guess you can assign a property and access it with dialogId.propertyName"

    Yes, exactly.


Log in to reply