Skip to content

Instantly share code, notes, and snippets.

@stephenquan
Last active April 9, 2018 21:24
Show Gist options
  • Save stephenquan/bfcaaf7e1c9a95e41079d69aa5c9fe0c to your computer and use it in GitHub Desktop.
Save stephenquan/bfcaaf7e1c9a95e41079d69aa5c9fe0c to your computer and use it in GitHub Desktop.
appstudio-devices-sample.qml
import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.1
import ArcGIS.AppFramework.Devices 1.0
Item {
property DeviceListModel deviceListModel: discoveryAgent.devices
property Device currentDevice
ColumnLayout {
anchors.fill: parent
anchors.margins: 10
ComboBox {
id: comboBox
Layout.fillWidth: true
model: deviceListModel
textRole: "name"
onCurrentTextChanged: selectDevice(deviceListModel.get(currentIndex))
}
ListView {
Layout.fillWidth: true
Layout.fillHeight: true
model: messagesListModel
delegate: Text { text: msg }
}
}
DeviceDiscoveryAgent {
id: discoveryAgent
}
DataSource {
id: dataSource
onReceivedDataChanged: log(receivedData.trim())
}
ListModel {
id: messagesListModel
}
Connections {
target: deviceListModel
onCountChanged: if (comboBox.currentIndex == -1 && deviceListModel.count > 0) { comboBox.currentIndex = 0; }
}
Component.onCompleted: discoveryAgent.start()
function selectDevice(device) {
if (currentDevice) currentDevice.connected = false;
currentDevice = device;
messagesListModel.clear();
JSON.stringify(device, undefined, 2).split("\n").forEach(log);
dataSource.source = currentDevice;
currentDevice.connected = true;
}
function log(txt) {
console.log(txt);
messagesListModel.append( { msg: txt } );
if (messagesListModel.count > 20) { messagesListModel.remove(0); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment