Your code should be fine. I think that the console.log() command is just in the wrong place.
If I understood correctly, there are two user interactions involved. The first one is the click on the mouse area, which opens the menu. The second one is a click on a particular menu item, which actually sets the test value.
Let's say you are performing the first interaction for the first time. At this point, the variable hasn't been set as you haven't clicked on a menu item yet. That is why the output of the console.log() command will be undefined. Then, you click on a menu item, which sets the variable. However, you won't be able to see the change until the menu is closed and you perform another click on the mouse area to make the menu pop up again.
I think you'll see that your code works if you move the console.log() command to a different place:
id: mouseArea1
property var testValue;
onClicked: {
menu.popup();
}
Menu {
id: menu
MenuItem {
text: "Option1"
onTriggered: {
mouseArea1.testValue= "25";
console.log(mouseArea1.testValue);
}
}
}
I don't know how many menu items you have in your actual code. Doing it this way, you'd need to add the same line to the onTriggered() signal handlers on all MenuItem instances, which could be annoying and bloat your code unnecessarily. So I'd suggest doing this instead:
id: mouseArea1
property var testValue;
onClicked: {
menu.popup();
}
Menu {
id: menu
MenuItem {
text: "Option1"
onTriggered: {
mouseArea1.testValue= "25";
}
}
onClosed: {
console.log(mouseArea1.testValue);
}
}
I am assuming that the menu is closed once you hit a menu item. The code above logs the variable once the menu emits a closed() signal. I haven't tried it myself, but it should work. :)