mirror of
https://github.com/status-im/dotherside.git
synced 2025-02-12 04:26:43 +00:00
[DQml] Improved the examples by matching with the one of NimQml
This allowed us to add also some missing functions in the QVariant interface and the QApplication type
This commit is contained in:
parent
7efdba5dce
commit
96552b1133
@ -10,5 +10,6 @@ add_library(${PROJECT_NAME} STATIC
|
||||
qqmlcontext.d
|
||||
chararray.d
|
||||
qguiapplication.d
|
||||
qapplication.d
|
||||
)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
@ -1,10 +1,17 @@
|
||||
extern(C)
|
||||
{
|
||||
// QApplication
|
||||
void dos_qapplication_create();
|
||||
void dos_qapplication_exec();
|
||||
void dos_qapplication_delete();
|
||||
void dos_qapplication_quit();
|
||||
|
||||
// QGuiApplication
|
||||
void dos_qguiapplication_create();
|
||||
void dos_qguiapplication_exec();
|
||||
void dos_qguiapplication_delete();
|
||||
|
||||
void dos_qguiapplication_quit();
|
||||
|
||||
// QQmlApplicationEngine
|
||||
void dos_qqmlapplicationengine_create(ref void*);
|
||||
void dos_qqmlapplicationengine_load(void*, immutable (char)* filename);
|
||||
@ -30,17 +37,23 @@ extern(C)
|
||||
|
||||
// QVariant
|
||||
void dos_qvariant_create(ref void*);
|
||||
void dos_qvariant_create_int(ref void*, int value);
|
||||
void dos_qvariant_create_bool(ref void*, bool value);
|
||||
void dos_qvariant_create_string(ref void*, immutable(char)* value);
|
||||
void dos_qvariant_create_int(ref void*, int);
|
||||
void dos_qvariant_create_bool(ref void*, bool);
|
||||
void dos_qvariant_create_string(ref void*, immutable(char)*);
|
||||
void dos_qvariant_create_qobject(ref void*, void*);
|
||||
void dos_qvariant_create_float(ref void*, float);
|
||||
void dos_qvariant_create_double(ref void*, double);
|
||||
void dos_qvariant_toInt(void*, ref int);
|
||||
void dos_qvariant_setInt(void*, int);
|
||||
void dos_qvariant_toBool(void*, ref bool);
|
||||
void dos_qvariant_setBool(void*, bool);
|
||||
void dos_qvariant_toString(void*, ref char*, ref int);
|
||||
void dos_qvariant_setString(void*, immutable(char)*);
|
||||
void dos_qvariant_isnull(void*, ref bool result);
|
||||
void dos_qvariant_toFloat(void*, ref float);
|
||||
void dos_qvariant_setFloat(void*, float);
|
||||
void dos_qvariant_toDouble(void*, ref double);
|
||||
void dos_qvariant_setDouble(void*, double);
|
||||
void dos_qvariant_isnull(void*, ref bool);
|
||||
void dos_qvariant_delete(void*);
|
||||
void dos_qvariant_assign(void*, void*);
|
||||
void dos_qvariant_setQObject(void*, void*);
|
||||
|
@ -1,5 +1,6 @@
|
||||
public import qvariant;
|
||||
public import qguiapplication;
|
||||
public import qapplication;
|
||||
public import qqmlcontext;
|
||||
public import qobject;
|
||||
public import qqmlapplicationengine;
|
||||
|
24
D/DQml/qapplication.d
Normal file
24
D/DQml/qapplication.d
Normal file
@ -0,0 +1,24 @@
|
||||
import dothersideinterface;
|
||||
|
||||
class QApplication
|
||||
{
|
||||
this()
|
||||
{
|
||||
dos_qapplication_create();
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
dos_qapplication_delete();
|
||||
}
|
||||
|
||||
void exec()
|
||||
{
|
||||
dos_qapplication_exec();
|
||||
}
|
||||
|
||||
void quit()
|
||||
{
|
||||
dos_qapplication_quit();
|
||||
}
|
||||
}
|
@ -16,4 +16,9 @@ class QGuiApplication
|
||||
{
|
||||
dos_qguiapplication_exec();
|
||||
}
|
||||
|
||||
void quit()
|
||||
{
|
||||
dos_qguiapplication_quit();
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ class QQmlApplicationEngine
|
||||
dos_qqmlapplicationengine_delete(data);
|
||||
}
|
||||
|
||||
public QQmlContext context()
|
||||
public QQmlContext rootContext()
|
||||
{
|
||||
void* contextData;
|
||||
dos_qqmlapplicationengine_context(data, contextData);
|
||||
|
@ -24,13 +24,23 @@ class QVariant
|
||||
{
|
||||
dos_qvariant_create_string(this.data, value.toStringz());
|
||||
}
|
||||
|
||||
|
||||
public this(float value)
|
||||
{
|
||||
dos_qvariant_create_float(this.data, value);
|
||||
}
|
||||
|
||||
public this(double value)
|
||||
{
|
||||
dos_qvariant_create_double(this.data, value);
|
||||
}
|
||||
|
||||
public this(QObject value)
|
||||
{
|
||||
dos_qvariant_create_qobject(this.data, value.rawData());
|
||||
}
|
||||
|
||||
public this(void* data, bool hasOwnership = false)
|
||||
public this(void* data, bool hasOwnership = false)
|
||||
{
|
||||
this.data = data;
|
||||
this.hasOwnership = hasOwnership;
|
||||
@ -67,6 +77,16 @@ class QVariant
|
||||
dos_qvariant_setQObject(this.data, value.rawData());
|
||||
}
|
||||
|
||||
public void setValue(float value)
|
||||
{
|
||||
dos_qvariant_setFloat(this.data, value);
|
||||
}
|
||||
|
||||
public void setValue(double value)
|
||||
{
|
||||
dos_qvariant_setDouble(this.data, value);
|
||||
}
|
||||
|
||||
public void getValue(ref int value)
|
||||
{
|
||||
value = toInt();
|
||||
@ -81,7 +101,17 @@ class QVariant
|
||||
{
|
||||
value = toString();
|
||||
}
|
||||
|
||||
public void getValue(ref float value)
|
||||
{
|
||||
value = toFloat();
|
||||
}
|
||||
|
||||
public void getValue(ref double value)
|
||||
{
|
||||
value = toDouble();
|
||||
}
|
||||
|
||||
public bool isNull()
|
||||
{
|
||||
bool result;
|
||||
@ -102,6 +132,20 @@ class QVariant
|
||||
dos_qvariant_toInt(this.data, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public float toFloat()
|
||||
{
|
||||
float result;
|
||||
dos_qvariant_toFloat(this.data, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public double toDouble()
|
||||
{
|
||||
double result;
|
||||
dos_qvariant_toDouble(this.data, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public override string toString()
|
||||
{
|
||||
|
@ -1,2 +1,3 @@
|
||||
add_subdirectory(Simple)
|
||||
add_subdirectory(ContactList)
|
||||
add_subdirectory(HelloWorld)
|
||||
add_subdirectory(SimpleData)
|
||||
add_subdirectory(SlotsAndProperties)
|
@ -1,75 +0,0 @@
|
||||
import dqml;
|
||||
import std.format;
|
||||
import std.stdio;
|
||||
|
||||
class Contact : QObject
|
||||
{
|
||||
this(string firstName, string lastName)
|
||||
{
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
|
||||
registerSlot("getFirstName", [QMetaType.String]);
|
||||
registerSlot("setFirstName", [QMetaType.Void, QMetaType.String]);
|
||||
registerSignal("firstNameChanged", [QMetaType.String]);
|
||||
registerProperty("firstName", QMetaType.String, "getFirstName", "setFirstName", "firstNameChanged");
|
||||
|
||||
registerSlot("getLastName", [QMetaType.String]);
|
||||
registerSlot("setLastName", [QMetaType.Void, QMetaType.String]);
|
||||
registerSignal("lastNameChanged", [QMetaType.String]);
|
||||
registerProperty("lastName", QMetaType.String, "getLastName", "setLastName", "lastNameChanged");
|
||||
}
|
||||
|
||||
public string getFirstName()
|
||||
{
|
||||
return this.firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(string firstName)
|
||||
{
|
||||
if (this.firstName != firstName)
|
||||
{
|
||||
this.firstName = firstName;
|
||||
emit("firstNameChanged", firstName);
|
||||
}
|
||||
}
|
||||
|
||||
public string getLastName()
|
||||
{
|
||||
writefln("Returning %s", this.lastName);
|
||||
return this.lastName;
|
||||
}
|
||||
|
||||
public void setLastName(string lastName)
|
||||
{
|
||||
if (this.lastName != lastName)
|
||||
{
|
||||
this.lastName = lastName;
|
||||
emit("lastNameChanged", lastName);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void onSlotCalled(QVariant slotName, QVariant[] arguments)
|
||||
{
|
||||
switch(slotName.toString())
|
||||
{
|
||||
case "getFirstName":
|
||||
arguments[0].setValue(getFirstName());
|
||||
break;
|
||||
case "setFirstName":
|
||||
setFirstName(arguments[1].toString());
|
||||
break;
|
||||
case "getLastName":
|
||||
arguments[0].setValue(getLastName());
|
||||
break;
|
||||
case "setLastName":
|
||||
setLastName(arguments[1].toString());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private string firstName;
|
||||
private string lastName;
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
import dqml;
|
||||
import contact;
|
||||
import std.stdio;
|
||||
import std.format;
|
||||
|
||||
class ContactList : QObject
|
||||
{
|
||||
this()
|
||||
{
|
||||
contacts = new Contact[](0);
|
||||
registerSlot("getCount", [QMetaType.Int]);
|
||||
registerSignal("countChanged", [QMetaType.Int]);
|
||||
registerProperty("count", QMetaType.Int, "getCount", null, "countChanged");
|
||||
registerSlot("getContact", [QMetaType.QObject, QMetaType.Int]);
|
||||
registerSlot("addContact", [QMetaType.Void, QMetaType.String, QMetaType.String]);
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
foreach (contact; this.contacts)
|
||||
destroy(contact);
|
||||
}
|
||||
|
||||
public void addContact(string firstName, string lastName)
|
||||
{
|
||||
writefln("addContact %s %s", firstName, lastName);
|
||||
this.contacts ~= new Contact(firstName, lastName);
|
||||
emit ("countChanged", new QVariant(getCount()));
|
||||
}
|
||||
|
||||
public QObject getContact(int index)
|
||||
{
|
||||
writefln("getContact index %d", index);
|
||||
return this.contacts[index];
|
||||
}
|
||||
|
||||
public int getCount()
|
||||
{
|
||||
auto result = cast(int)this.contacts.length;
|
||||
writefln("getCount %d", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
override void onSlotCalled(QVariant slotName, QVariant[] arguments)
|
||||
{
|
||||
switch(slotName.toString())
|
||||
{
|
||||
case "addContact":
|
||||
assert(arguments.length == 3);
|
||||
addContact(arguments[1].toString(),
|
||||
arguments[2].toString());
|
||||
break;
|
||||
case "getContact":
|
||||
arguments[0].setValue(getContact(arguments[1].toInt()));
|
||||
break;
|
||||
case "getCount":
|
||||
arguments[0].setValue(getCount());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Contact[] contacts;
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import std.traits;
|
||||
import std.conv;
|
||||
import std.functional;
|
||||
import core.memory;
|
||||
import dqml;
|
||||
import contactlist;
|
||||
import contact;
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto app = new QGuiApplication;
|
||||
scope(exit) destroy(app);
|
||||
|
||||
auto contactList = new ContactList();
|
||||
scope(exit) destroy(contactList);
|
||||
contactList.addContact("John", "Doo");
|
||||
contactList.addContact("Peter", "Roger");
|
||||
|
||||
auto qmlApplicationEngine = new QQmlApplicationEngine;
|
||||
scope(exit) destroy(qmlApplicationEngine);
|
||||
qmlApplicationEngine.context().setContextProperty("contactList", new QVariant(contactList));
|
||||
qmlApplicationEngine.load("main.qml");
|
||||
|
||||
app.exec();
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
import QtQuick 2.3
|
||||
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
|
||||
ListView {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
model: contactList.count
|
||||
delegate: Label {
|
||||
property QtObject contact: contactList.getContact(index)
|
||||
text: contact.firstName + " " + contact.lastName
|
||||
}
|
||||
}
|
||||
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Item { Layout.fillWidth: true }
|
||||
Button {
|
||||
Layout.preferredWidth: 100
|
||||
text: "Add"
|
||||
onClicked: contactList.addContact("NewFirstName", "NewLastName")
|
||||
}
|
||||
Item { width: 10; }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project (Simple C D )
|
||||
add_executable(${PROJECT_NAME} myqobject.d main.d)
|
||||
project (HelloWorld C D )
|
||||
add_executable(${PROJECT_NAME} main.d)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../../DQml"
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
@ -9,3 +9,4 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/../../DQml/libDQml.a"
|
||||
)
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
19
D/Examples/HelloWorld/main.d
Normal file
19
D/Examples/HelloWorld/main.d
Normal file
@ -0,0 +1,19 @@
|
||||
import std.stdio;
|
||||
import dqml;
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto app = new QGuiApplication();
|
||||
scope(exit) destroy(app);
|
||||
|
||||
auto engine = new QQmlApplicationEngine();
|
||||
scope(exit) destroy(engine);
|
||||
engine.load("main.qml");
|
||||
|
||||
app.exec();
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
12
D/Examples/HelloWorld/main.qml
Normal file
12
D/Examples/HelloWorld/main.qml
Normal file
@ -0,0 +1,12 @@
|
||||
import QtQuick 2.2
|
||||
import QtQuick.Controls 1.2
|
||||
import QtQuick.Layouts 1.1
|
||||
import QtQuick.Window 2.1
|
||||
|
||||
ApplicationWindow
|
||||
{
|
||||
width: 400
|
||||
height: 300
|
||||
title: "Hello World"
|
||||
Component.onCompleted: visible = true
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import std.traits;
|
||||
import std.conv;
|
||||
import std.functional;
|
||||
import core.memory;
|
||||
import dqml;
|
||||
import myqobject;
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto app = new QGuiApplication;
|
||||
scope(exit) destroy(app);
|
||||
|
||||
auto myQObject = new MyQObject();
|
||||
scope(exit) destroy(myQObject);
|
||||
|
||||
auto qmlApplicationEngine = new QQmlApplicationEngine;
|
||||
scope(exit) destroy(qmlApplicationEngine);
|
||||
qmlApplicationEngine.context().setContextProperty("myQObject", new QVariant(myQObject));
|
||||
qmlApplicationEngine.load("main.qml");
|
||||
|
||||
app.exec();
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
import QtQuick 2.3
|
||||
import QtQuick.Controls 1.2
|
||||
import QtQuick.Layouts 1.1
|
||||
import QtQuick.Window 2.1
|
||||
|
||||
ApplicationWindow {
|
||||
width: 400
|
||||
height: 300
|
||||
visible: true
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
|
||||
Label {
|
||||
text: "Current name iss:" + myQObject.name
|
||||
}
|
||||
|
||||
TextField {
|
||||
id: textField
|
||||
}
|
||||
|
||||
Button {
|
||||
text: "Change Name"
|
||||
onClicked: myQObject.name = textField.text
|
||||
}
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
import std.stdio;
|
||||
import std.format;
|
||||
import dqml;
|
||||
|
||||
class MyQObject : QObject
|
||||
{
|
||||
this()
|
||||
{
|
||||
m_name = "John Doo";
|
||||
registerSlot("name", [QMetaType.String]);
|
||||
registerSlot("setName", [QMetaType.Void, QMetaType.String]);
|
||||
registerSignal("nameChanged", [QMetaType.String]);
|
||||
registerProperty("name", QMetaType.String, "name", "setName", "nameChanged");
|
||||
}
|
||||
|
||||
public string name()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
public void setName(string name)
|
||||
{
|
||||
if (name == m_name)
|
||||
return;
|
||||
m_name = name;
|
||||
emit("nameChanged", new QVariant(name));
|
||||
}
|
||||
|
||||
override void onSlotCalled(QVariant slotName, QVariant[] arguments)
|
||||
{
|
||||
switch(slotName.toString())
|
||||
{
|
||||
case "name":
|
||||
arguments[0].setValue(name());
|
||||
break;
|
||||
case "setName":
|
||||
setName(arguments[1].toString());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private string m_name;
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project (ContactList C D )
|
||||
add_executable(${PROJECT_NAME} contact.d contactlist.d main.d)
|
||||
project (SimpleData C D )
|
||||
add_executable(${PROJECT_NAME} main.d)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../../DQml"
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
@ -9,3 +9,4 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/../../DQml/libDQml.a"
|
||||
)
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
36
D/Examples/SimpleData/main.d
Normal file
36
D/Examples/SimpleData/main.d
Normal file
@ -0,0 +1,36 @@
|
||||
import std.stdio;
|
||||
import dqml;
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto app = new QGuiApplication();
|
||||
scope(exit) destroy(app);
|
||||
|
||||
auto engine = new QQmlApplicationEngine();
|
||||
scope(exit) destroy(engine);
|
||||
|
||||
auto qVar1 = new QVariant(10);
|
||||
scope(exit) destroy(qVar1);
|
||||
|
||||
auto qVar2 = new QVariant("Hello World");
|
||||
scope(exit) destroy(qVar1);
|
||||
|
||||
auto qVar3 = new QVariant(false);
|
||||
scope(exit) destroy(qVar1);
|
||||
|
||||
auto qVar4 = new QVariant(3.5f);
|
||||
scope(exit) destroy(qVar1);
|
||||
|
||||
engine.rootContext().setContextProperty("qVar1", qVar1);
|
||||
engine.rootContext().setContextProperty("qVar2", qVar2);
|
||||
engine.rootContext().setContextProperty("qVar3", qVar3);
|
||||
engine.rootContext().setContextProperty("qVar4", qVar4);
|
||||
engine.load("main.qml");
|
||||
|
||||
app.exec();
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
21
D/Examples/SimpleData/main.qml
Normal file
21
D/Examples/SimpleData/main.qml
Normal file
@ -0,0 +1,21 @@
|
||||
import QtQuick 2.2
|
||||
import QtQuick.Controls 1.2
|
||||
import QtQuick.Layouts 1.1
|
||||
import QtQuick.Window 2.1
|
||||
|
||||
ApplicationWindow
|
||||
{
|
||||
width: 400
|
||||
height: 300
|
||||
title: "SimpleData"
|
||||
Component.onCompleted: visible = true
|
||||
|
||||
ColumnLayout
|
||||
{
|
||||
anchors.fill: parent
|
||||
SpinBox { value: qVar1}
|
||||
TextField { text: qVar2}
|
||||
CheckBox { checked: qVar3}
|
||||
SpinBox { value: qVar4; decimals: 1 }
|
||||
}
|
||||
}
|
12
D/Examples/SlotsAndProperties/CMakeLists.txt
Normal file
12
D/Examples/SlotsAndProperties/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project (SlotsAndProperties C D )
|
||||
add_executable(${PROJECT_NAME} main.d contact.d)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../../DQml"
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/../../../DOtherSide/DOtherSide/libDOtherSide.so"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/../../DQml/libDQml.a"
|
||||
)
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
46
D/Examples/SlotsAndProperties/contact.d
Normal file
46
D/Examples/SlotsAndProperties/contact.d
Normal file
@ -0,0 +1,46 @@
|
||||
import dqml;
|
||||
|
||||
class Contact : QObject
|
||||
{
|
||||
this()
|
||||
{
|
||||
registerSlot("getName", [QMetaType.String]);
|
||||
registerSlot("setName", [QMetaType.Void, QMetaType.String]);
|
||||
registerSignal("nameChanged", [QMetaType.String]);
|
||||
registerProperty("name", QMetaType.String, "getName", "setName", "nameChanged");
|
||||
}
|
||||
|
||||
~this() {}
|
||||
|
||||
|
||||
public string getName()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
public void setName(string name)
|
||||
{
|
||||
if (m_name != name)
|
||||
{
|
||||
m_name = name;
|
||||
emit("nameChanged", name);
|
||||
}
|
||||
}
|
||||
|
||||
override protected void onSlotCalled(QVariant slotName, QVariant[] arguments)
|
||||
{
|
||||
switch (slotName.toString())
|
||||
{
|
||||
case "getName":
|
||||
arguments[0].setValue(getName());
|
||||
break;
|
||||
case "setName":
|
||||
setName(arguments[1].toString());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private string m_name;
|
||||
}
|
27
D/Examples/SlotsAndProperties/main.d
Normal file
27
D/Examples/SlotsAndProperties/main.d
Normal file
@ -0,0 +1,27 @@
|
||||
import std.stdio;
|
||||
import dqml;
|
||||
import contact;
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto app = new QGuiApplication();
|
||||
scope(exit) destroy(app);
|
||||
|
||||
auto contact = new Contact();
|
||||
scope(exit) destroy(contact);
|
||||
|
||||
auto variant = new QVariant(contact);
|
||||
scope(exit) destroy(variant);
|
||||
|
||||
auto engine = new QQmlApplicationEngine();
|
||||
scope(exit) destroy(engine);
|
||||
|
||||
engine.rootContext().setContextProperty("contact", variant);
|
||||
engine.load("main.qml");
|
||||
app.exec();
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
24
D/Examples/SlotsAndProperties/main.qml
Normal file
24
D/Examples/SlotsAndProperties/main.qml
Normal file
@ -0,0 +1,24 @@
|
||||
import QtQuick 2.2
|
||||
import QtQuick.Controls 1.2
|
||||
import QtQuick.Layouts 1.1
|
||||
import QtQuick.Window 2.1
|
||||
|
||||
ApplicationWindow {
|
||||
width: 400
|
||||
height: 300
|
||||
visible: true
|
||||
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
|
||||
Label { text: "Current name is:" + contact.name }
|
||||
|
||||
TextField { id: textField }
|
||||
|
||||
Button{
|
||||
text: "Change Name"
|
||||
onClicked: contact.name = textField.text
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user