caiwc 9 年 前
コミット
637b1e03c7

+ 1 - 0
SUMMARY.md

@@ -128,6 +128,7 @@
        * [使用动作(Using Actions)](extending_qml_with_c++/using_actions.md)
        * [使用动作(Using Actions)](extending_qml_with_c++/using_actions.md)
        * [格式化表格(Formatting the Table)](extending_qml_with_c++/formatting_the_table.md)
        * [格式化表格(Formatting the Table)](extending_qml_with_c++/formatting_the_table.md)
        * [读取数据(Reading Data)](extending_qml_with_c++/reading_data.md)
        * [读取数据(Reading Data)](extending_qml_with_c++/reading_data.md)
+       * [写入数据(Writing Data)](extending_qml_with_c++/writing_data.md)
 * [其它(Other)](other/README.md)
 * [其它(Other)](other/README.md)
    * [协作校正](other/collaboration_correction.md)
    * [协作校正](other/collaboration_correction.md)
 
 

+ 2 - 1
extending_qml_with_c++/formatting_the_table.md

@@ -1,6 +1,7 @@
 # 格式化表格(Formatting the Table)
 # 格式化表格(Formatting the Table)
 
 
-城市数据的内容应该被现实在一个表格中。我们使用TableView控制并定义4列:城市,国家,面积,人口。每一列都是典型的TableViewColumn。然后我们添加列的标识并移除要求自定义列代理的操作。
+城市数据的内容应该被现实在一个表格中。我们使用```TableView```
+控制并定义4列:城市,国家,面积,人口。每一列都是典型的```TableViewColumn```。然后我们添加列的标识并移除要求自定义列代理的操作。
 
 
 ```
 ```
 TableView {
 TableView {

+ 1 - 1
extending_qml_with_c++/reading_data.md

@@ -1,6 +1,6 @@
 # 读取数据(Reading Data)
 # 读取数据(Reading Data)
 
 
-我们让打开动作打开一个文件对话框。当用户已选择一个文件后,在文件对话框上的onAccepted方法被调用。这里我们调用readDocument()函数。readDocument函数将来自文件对话框的地址设置到我们的FileIO对象,并调用read()方法。从FileIO中加载的文本使用JSON.parse()方法解析,并将结果对象作为数据模型直接设置到表格视图上。这样非常方便。
+我们让打开动作打开一个文件对话框。当用户已选择一个文件后,在文件对话框上的```onAccepted```方法被调用。这里我们调用```readDocument()```函数。```readDocument```函数将来自文件对话框的地址设置到我们的```FileIO```对象,并调用```read()```方法。从```FileIO```中加载的文本使用```JSON.parse()```方法解析,并将结果对象作为数据模型直接设置到表格视图上。这样非常方便。
 
 
 ```
 ```
 Action {
 Action {

+ 23 - 0
extending_qml_with_c++/writing_data.md

@@ -0,0 +1,23 @@
+# 写入数据(Writing Data)
+
+我们连接保存动作到saveDocument()函数来保存文档。保存文档函数从视图中取出模型,模型是一个JS对象,并使用JSON.stringify()函数将它转换为一个字符串。将结果字符串设置到FileIO对象的文本属性中,并调用write()来保存数据到磁盘中。在stringify函数上参数null和4将会使用4个空格缩进格式化JSON数据结果。这只是为了保存文档更好阅读。
+
+```
+Action {
+    id: save
+    ...
+    onTriggered: {
+        saveDocument()
+    }
+}
+
+function saveDocument() {
+    var data = view.model
+    io.text = JSON.stringify(data, null, 4)
+    io.write()
+}
+
+FileIO {
+    id: io
+}
+```