Does anyone have sample code for how to open a QHMI page based on the value in an external tag. Regards Ross
Hello Ross,
here is an example. The first handler opens a popup when a BOOL tag changes to true, the second one opens a page depending on the number in an INT tag. Replace the tag and page names with your own.
// Open a popup when a BOOL tag changes to true
Events.onVarChanged("PLC.ShowPopup", function (args) {
if (args.variable.value === "true" && args.variable.lastvalue !== "true") {
mainInterface.openView("MyPopup");
}
});
// Open a page depending on the number in an INT tag
Events.onVarChanged("PLC.PageNumber", function (args) {
var pages = { "1": "Overview", "2": "Motors", "3": "Alarms" };
var page = pages[args.variable.value];
if (page && args.variable.value !== args.variable.lastvalue) {
mainInterface.openView(page);
}
});
Tag values arrive in the script as text, so a BOOL tag contains "true" or "false" (see mainInterface.getVariable in https://docs.quickhmi.com/en/dokumentation/definition-en/api-functions/ ). That is why the example compares with "true" in quotes: if (value == true) never matches, and if (value) would also match "false". The check against lastvalue makes sure the page only opens when the value has actually changed and not on every update of the tag ( https://docs.quickhmi.com/en/dokumentation/definition-en/variable-change-events/ ).
openView only needs the name of the page, it works for pages and popups, and the group the page is in doesn't matter.
Best regards


