1

I want to click a button, then popup a dialog box. The Dialog is from Qml, has a label which is made of a variable number from another JavaScript file. When the variable changes, the dialog should repaint, the new number will display on the dialog box.

MyDlg.qml:

import "MyJs.js" as MyJs

Window {

    id: myDialog
    width: 300
    height: 300

    TabView {
        id:myTabView
        width: parent.width
        height: parent.height

        Tab {
            title: "tab 1"
            id: myTab1
            text: MyJs.displayText
            }
     }
}

MyJs.js:

var displayText = "0";
Filip Hazubski
  • 1,668
  • 1
  • 17
  • 33
Karen
  • 31
  • 2

1 Answers1

2

Bindings don't work between QML and separate JavaScript files, only inline JavaScript expressions. I can't find any documentation that explicitly states this, but it has also been mentioned in previous answers.

If you don't want to step into C++, use a QML singleton (another answer to that question). Here's an example of how you'd use it:

MyDialog.qml

import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Window 2.0

import "."

Window {
    id: myDialog
    width: 300
    height: 300


    TabView {
        id:myTabView
        width: parent.width
        height: parent.height

        Tab {
            title: MySingleton.displayText
            id: myTab1

            Button {
                text: "Click to change singleton property"
                onClicked: MySingleton.displayText = "Hello"
            }
        }
    }
}

MySingleton.qml

pragma Singleton

import QtQml 2.0

QtObject {
    property string displayText: ""
}

qmldir

singleton MySingleton MySingleton.qml

More information:

Community
  • 1
  • 1
Mitch
  • 23,716
  • 9
  • 83
  • 122