Added an example of using the QtObject macro

This commit is contained in:
Will Szumski 2015-01-04 21:58:32 +00:00
parent cdf02a4251
commit 7c4d54a95b
3 changed files with 95 additions and 0 deletions

View 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)

View 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()

View 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
}
}
}
}