Finished the initial doc
This commit is contained in:
parent
50eeefa5c4
commit
328355fff8
|
@ -167,7 +167,6 @@ The magic is in the Contact.nim file
|
|||
.. code-block:: nim
|
||||
:file: ../Examples/SlotsAndProperties/Contact.nim
|
||||
|
||||
|
||||
What First we declare a QObject subclass and provide a simple
|
||||
new method where:
|
||||
1. invoke the ``create()`` procedure. This invoke the C++ bridge and allocate
|
||||
|
@ -192,4 +191,44 @@ If the invoked slot has a return value this is always in the index position
|
|||
|
||||
|
||||
Example 4: QtObject macro
|
||||
-------------------------
|
||||
The previous example shows how to create simple QObject however writing
|
||||
all those ``register`` procs and writing the ``onSlotCalled`` method
|
||||
become boring pretty soon.
|
||||
|
||||
Furthermore all this information can be automatically generated.
|
||||
For this purpose you can import the NimQmlMacros module that provide
|
||||
the QtObject macro.
|
||||
|
||||
Let's begin as usual with both the main.nim and main.qml files
|
||||
|
||||
``Examples/QtObjectMacro/main.nim``
|
||||
|
||||
.. code-block:: nim
|
||||
:file: ../Examples/QtObjectMacro/main.nim
|
||||
|
||||
|
||||
``Examples/QtObjectMacro/main.qml``
|
||||
|
||||
.. code-block:: qml
|
||||
:file: ../Examples/QtObjectMacro/main.qml
|
||||
|
||||
Nothing new in both the ``main.nim`` and ``main.qml`` in respect to
|
||||
the previous example. What changed is the Contact object
|
||||
|
||||
``Examples/QtObjectMacro/Contact.nim``
|
||||
|
||||
.. code-block:: nim
|
||||
:file: ../Examples/QtObjectMacro/Contact.nim
|
||||
|
||||
In details:
|
||||
1. Each QObject is defined inside the QtObject macro
|
||||
2. Each slot is annotated with the ``{.slot.}`` macro
|
||||
3. Each signal is annotated with the ``{.signal.}`` macro
|
||||
4. Each property is created with the ``QtProperty`` macro
|
||||
|
||||
The ``QtProperty`` macro has the following syntax
|
||||
|
||||
.. code-block:: nim
|
||||
QtProperty nameOfProperty of typeOfProperty
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
add_subdirectory(HelloWorld)
|
||||
add_subdirectory(SimpleData)
|
||||
add_subdirectory(Simple)
|
||||
add_subdirectory(SlotsAndProperties)
|
||||
add_subdirectory(SlotsAndProperties)
|
||||
add_subdirectory(QtObjectMacro)
|
|
@ -1,2 +1,2 @@
|
|||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
add_nim_executable(TARGET Simple SOURCES main.nim PATHS ../../NimQml)
|
||||
add_nim_executable(TARGET QtObjectMacro SOURCES main.nim PATHS ../../NimQml)
|
|
@ -0,0 +1,27 @@
|
|||
## 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 Contact = ref object of QObject
|
||||
m_name: string
|
||||
|
||||
template newContact*(): Contact =
|
||||
var result = Contact(m_name: "initialName")
|
||||
result.create
|
||||
result
|
||||
|
||||
method getName*(contact: Contact): string {.slot.} =
|
||||
result = contact.m_name
|
||||
|
||||
method nameChanged*(contact: Contact) {.signal.}
|
||||
|
||||
method setName*(contact: Contact, name: string) {.slot.} =
|
||||
if contact.m_name != name:
|
||||
contact.m_name = name
|
||||
contact.nameChanged()
|
||||
|
||||
QtProperty name of string:
|
||||
read = getName
|
||||
write = setName
|
||||
notify = nameChanged
|
|
@ -0,0 +1,27 @@
|
|||
import NimQml
|
||||
import Contact
|
||||
|
||||
proc mainProc() =
|
||||
var app: QApplication
|
||||
app.create()
|
||||
defer: app.delete()
|
||||
|
||||
var contact = newContact()
|
||||
defer: contact.delete()
|
||||
|
||||
var engine: QQmlApplicationEngine
|
||||
engine.create()
|
||||
defer: engine.delete()
|
||||
|
||||
var variant: QVariant
|
||||
variant.create(contact)
|
||||
defer: variant.delete()
|
||||
|
||||
var rootContext: QQmlContext = engine.rootContext()
|
||||
rootContext.setContextProperty("contact", variant)
|
||||
engine.load("main.qml")
|
||||
app.exec()
|
||||
|
||||
when isMainModule:
|
||||
mainProc()
|
||||
|
|
@ -16,7 +16,7 @@ ApplicationWindow
|
|||
|
||||
Label
|
||||
{
|
||||
text: "Current name is:" + myQObject.name
|
||||
text: "Current name is:" + contact.name
|
||||
}
|
||||
|
||||
TextField
|
||||
|
@ -27,10 +27,7 @@ ApplicationWindow
|
|||
Button
|
||||
{
|
||||
text: "Change Name"
|
||||
onClicked: {
|
||||
console.log("QML:", textField.text)
|
||||
myQObject.name = textField.text
|
||||
}
|
||||
onClicked: contact.name = textField.text
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
import NimQml
|
||||
import macros
|
||||
import typeinfo
|
||||
|
||||
type MyQObject = ref object of QObject
|
||||
m_name: string
|
||||
|
||||
method getName(myQObject: MyQObject): string =
|
||||
result = myQObject.m_name
|
||||
|
||||
method setName(myQObject: MyQObject, name: string) =
|
||||
if myQObject.m_name != name:
|
||||
myQObject.m_name = name
|
||||
myQObject.emit("nameChanged")
|
||||
|
||||
method onSlotCalled(myQObject: MyQObject, slotName: string, args: openarray[QVariant]) =
|
||||
case slotName:
|
||||
of "getName":
|
||||
args[0].stringVal = myQObject.getName()
|
||||
of "setName":
|
||||
myQObject.setName(args[1].stringVal)
|
||||
else:
|
||||
discard()
|
||||
|
||||
proc mainProc() =
|
||||
var app: QApplication
|
||||
app.create()
|
||||
defer: app.delete()
|
||||
|
||||
var myQObject = MyQObject()
|
||||
myQObject.create()
|
||||
defer: myQObject.delete()
|
||||
myQObject.m_name = "InitialName"
|
||||
myQObject.registerSlot("getName", [QMetaType.QString])
|
||||
myQObject.registerSlot("setName", [QMetaType.Void, QMetaType.QString])
|
||||
myQObject.registerSignal("nameChanged", [QMetaType.Void])
|
||||
myQObject.registerProperty("name", QMetaType.QString, "getName", "setName", "nameChanged")
|
||||
|
||||
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()
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
add_nim_executable(TARGET Main SOURCES main.nim PATHS ../../NimQml)
|
|
@ -1,57 +0,0 @@
|
|||
## 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()
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
## 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
|
||||
|
||||
type Contact = ref object of QObject
|
||||
|
|
|
@ -197,7 +197,7 @@ template declareOnSlotCalled(typ: typedesc): stmt =
|
|||
discard
|
||||
|
||||
template prototypeCreate(typ: typedesc): stmt =
|
||||
template create(myQObject: var typ) =
|
||||
template create*(myQObject: var typ) =
|
||||
var super = (typ.superType())(myQObject)
|
||||
procCall create(super)
|
||||
|
||||
|
|
Loading…
Reference in New Issue