mirror of
https://github.com/status-im/dotherside.git
synced 2025-02-12 12:46:24 +00:00
Added an example of using the QtObject macro
This commit is contained in:
parent
cdf02a4251
commit
7c4d54a95b
2
Nim/Examples/SimpleMacro/CMakeLists.txt
Normal file
2
Nim/Examples/SimpleMacro/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
add_nim_executable(TARGET Main SOURCES main.nim PATHS ../../NimQml)
|
57
Nim/Examples/SimpleMacro/main.nim
Normal file
57
Nim/Examples/SimpleMacro/main.nim
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
## This example replicates the functionality of Examples/Simple but uses
|
||||||
|
## the helper macro, QtObject, from NimQmlMacros, to remove some of the boiler plate.
|
||||||
|
## Please note we are using templates where ordinarily we would like to use procedures
|
||||||
|
## due to bug: https://github.com/Araq/Nim/issues/1821
|
||||||
|
|
||||||
|
import NimQml, NimQmlMacros
|
||||||
|
|
||||||
|
QtObject:
|
||||||
|
type MyQObject = ref object of QObject
|
||||||
|
m_name: string
|
||||||
|
|
||||||
|
template newMyQObject(initial: string): MyQObject =
|
||||||
|
var result = MyQObject(m_name: initial)
|
||||||
|
result.create
|
||||||
|
result
|
||||||
|
|
||||||
|
method getName(myQObject: MyQObject): string {.slot.} =
|
||||||
|
echo "nameChanged"
|
||||||
|
result = myQObject.m_name
|
||||||
|
|
||||||
|
method nameChanged(myQObject: MyQObject) {.signal.}
|
||||||
|
|
||||||
|
method setName(myQObject: MyQObject, name: string) {.slot.} =
|
||||||
|
echo "setName"
|
||||||
|
if myQObject.m_name != name:
|
||||||
|
myQObject.m_name = name
|
||||||
|
myQObject.nameChanged()
|
||||||
|
|
||||||
|
QtProperty name of string:
|
||||||
|
read = getName
|
||||||
|
write = setName
|
||||||
|
notify = nameChanged
|
||||||
|
|
||||||
|
proc mainProc() =
|
||||||
|
var app: QApplication
|
||||||
|
app.create()
|
||||||
|
defer: app.delete()
|
||||||
|
|
||||||
|
var myQObject = newMyQObject("InitialName")
|
||||||
|
defer: myQObject.delete()
|
||||||
|
|
||||||
|
var engine: QQmlApplicationEngine
|
||||||
|
engine.create()
|
||||||
|
defer: engine.delete()
|
||||||
|
|
||||||
|
var variant: QVariant
|
||||||
|
variant.create(myQObject)
|
||||||
|
defer: variant.delete()
|
||||||
|
|
||||||
|
var rootContext: QQmlContext = engine.rootContext()
|
||||||
|
rootContext.setContextProperty("myQObject", variant)
|
||||||
|
engine.load("main.qml")
|
||||||
|
app.exec()
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
mainProc()
|
||||||
|
|
36
Nim/Examples/SimpleMacro/main.qml
Normal file
36
Nim/Examples/SimpleMacro/main.qml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import QtQuick 2.2
|
||||||
|
import QtQuick.Controls 1.2
|
||||||
|
import QtQuick.Layouts 1.1
|
||||||
|
import QtQuick.Window 2.1
|
||||||
|
|
||||||
|
ApplicationWindow
|
||||||
|
{
|
||||||
|
width: 400
|
||||||
|
height: 300
|
||||||
|
|
||||||
|
Component.onCompleted: visible = true
|
||||||
|
|
||||||
|
ColumnLayout
|
||||||
|
{
|
||||||
|
anchors.fill: parent
|
||||||
|
|
||||||
|
Label
|
||||||
|
{
|
||||||
|
text: "Current name is:" + myQObject.name
|
||||||
|
}
|
||||||
|
|
||||||
|
TextField
|
||||||
|
{
|
||||||
|
id: textField
|
||||||
|
}
|
||||||
|
|
||||||
|
Button
|
||||||
|
{
|
||||||
|
text: "Change Name"
|
||||||
|
onClicked: {
|
||||||
|
console.log("QML:", textField.text)
|
||||||
|
myQObject.name = textField.text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user