M
M
Maxim Rudnev2017-01-24 23:49:17
Qt
Maxim Rudnev, 2017-01-24 23:49:17

How to create object from .js file in QML?

What am I doing wrong?
There is a js file, a primitive constructor function.
in qml I want to create an object from this js file by allocating memory and fill it.

function device( ip,name,port,community,oid_count)
{
    this._ip=ip;
    this._name=name;
    this._port=port;
    this._community=community;
    this._oid_count=oid_count;
}

qml:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import "device_obj.js" as Device
ApplicationWindow {
    visible: true
    width: 640
    height: 480
    color: "#a3a3a3"
    title: qsTr("Hello World")

Button {
        id: button_ok
        x: 468
        y: 423
        text: qsTr("Button_OK")
        onClicked: 
{

            var ip="123";
            var name="123";
            var port="123";
            var community="123";
            var oid_count="123"
            var device= new Device(ip,name,port,community,oid_count);


        }
    }




}

Error on memory allocation line via NEW. TypeError: Type error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacob E, 2017-01-25
@stigmt

There is a special, street js.
We create the Device.qml file, inside it we write:

Item {
    property string ip
    property string port
    ...
    // Тут может быть и функция-конструктор
}

Next, we either create
Device {
    id: device
}

and fill in its fields in the button handler.
Or create an object dynamically, doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreat...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question