Skip to content

Instantly share code, notes, and snippets.

@stephenquan
Created November 1, 2018 01:29
Show Gist options
  • Save stephenquan/3fc5fa370b2a3fd8d7fc6aa3d56ef7d8 to your computer and use it in GitHub Desktop.
Save stephenquan/3fc5fa370b2a3fd8d7fc6aa3d56ef7d8 to your computer and use it in GitHub Desktop.
Evaluator.qml
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Evaluator")
property string errorString: null
ColumnLayout {
anchors.fill: parent
anchors.margins: 10
Flickable {
id: inputFlickable
Layout.fillWidth: true
Layout.fillHeight: true
contentWidth: inputTextArea.width
contentHeight: inputTextArea.height
TextArea {
id: inputTextArea
selectByMouse: true
text: "1 + 2 + 3"
onTextChanged: evaluate()
}
}
Flickable {
id: outputFlickable
Layout.fillWidth: true
Layout.fillHeight: true
contentWidth: outputTextArea.width
contentHeight: outputTextArea.height
TextArea {
id: outputTextArea
readOnly: true
}
}
Text {
text: errorString
color: "red"
}
}
function evaluate() {
errorString = null;
outputTextArea.text = "";
try {
outputTextArea.text = JSON.stringify(eval(inputTextArea.text), undefined, 2);
} catch (err) {
errorString = err.message;
}
}
Component.onCompleted: evaluate()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment