第二卷如何更快速的放弃,注重的是C++和QML的交互

<1>记事本。。

(1) 先测试下不在QML创建C++对象,仅仅在main.cpp添加一个属性函数供调用. 注意只使用槽函数来做到。

TextStreamLoader.h

  1. #ifndef TEXTSTREAMLOADER_H
  2. #define TEXTSTREAMLOADER_H
  3.  
  4. #include <QObject>
  5. #include <QTextStream>
  6. #include <QDebug>
  7. class TextStreamLoader : public QObject
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit TextStreamLoader(QObject *parent = );
  12. void test2(){qDebug()<<"test 2 without slots";}
  13. signals:
  14. void signal_readFile(QString buffer);
  15. void signal_error(QString errorMsg);
  16. void signal_saveFile(QString file,QString buffer);
  17. public slots:
  18. void slot_readFile(QString file);
  19. void slot_saveFile(QString file,QString buffer);
  20. void slot_test(){qDebug() << "test C++";}
  21. QString slot_getBuffer();
  22.  
  23. private:
  24. QString _buffer;
  25. };
  26.  
  27. #endif // TEXTSTREAMLOADER_H

TextStreamLoader.cpp

  1. #include "TextStreamLoader.h"
  2. #include <QFile>
  3. #include <QUrl>
  4. TextStreamLoader::TextStreamLoader(QObject *parent) : QObject(parent)
  5. {
  6. qDebug() << "Construct the TextStreamLoader";
  7. connect(this,&TextStreamLoader::signal_saveFile,
  8. this,&TextStreamLoader::slot_saveFile);
  9. }
  10.  
  11. void TextStreamLoader::slot_readFile(QString file) // read a file to the _buffer
  12. {
  13.  
  14. QUrl url(file);
  15. QString localFile = url.toLocalFile();
  16.  
  17. QFile rfile(localFile);
  18. if(!rfile.open(QIODevice::ReadOnly))
  19. {
  20. QString errorMsg = "Could not open " + file + "\n";
  21. qDebug() << errorMsg;
  22. emit signal_error(errorMsg);
  23. return ;
  24. }
  25.  
  26. QTextStream in(&rfile);
  27. _buffer = in.readAll();
  28. emit signal_readFile(_buffer);
  29.  
  30. rfile.close();
  31.  
  32. }
  33.  
  34. void TextStreamLoader::slot_saveFile(QString file, QString buffer)
  35. {
  36. QUrl url(file);
  37. QString localFile = url.toLocalFile();
  38. QFile wfile(localFile);
  39. if(!wfile.open(QFile::WriteOnly))
  40. {
  41. QString errorMsg = "Could not open " + localFile + "\n";
  42. qDebug() <<errorMsg;
  43. emit signal_error(errorMsg);
  44. return ;
  45. }
  46.  
  47. QTextStream out(&wfile);
  48. out << buffer;
  49. wfile.close();
  50. }
  51.  
  52. QString TextStreamLoader::slot_getBuffer()
  53. {
  54. return _buffer;
  55. }

main.cpp

  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlContext>
  4. #include "TextStreamLoader.h"
  5. int main(int argc, char *argv[])
  6. {
  7. QGuiApplication app(argc, argv);
  8.  
  9. QQmlApplicationEngine engine;
  10. QQmlContext *context = engine.rootContext();
  11.  
  12. // 注意对象是在C++里构建
  13. TextStreamLoader stream_01;
  14. context->setContextProperty("stream_01",&stream_01);
  15. // 构建完C++对象
  16.  
  17. // 加载我们的QML界面,只能调用槽函数
  18. qDebug() << "load the main.qml";
  19. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  20.  
  21. qDebug() <<engine.rootObjects()[]->objectName(); // this will be debug "Houdini"
  22.  
  23. return app.exec();
  24. }

main.qml 用最简单的测试下我们的TextStreamLoader 里面的 "test()槽函数",一定要是槽函数才能被调用。

main.qml全部都是通过调用C++的对象的槽函数,而C++对象是在main.cpp创建,所以在qml随时可以访问 槽函数。

  1. import QtQuick 2.6
  2. import QtQuick.Window 2.2
  3. import QtQuick.Controls 2.1
  4. import QtQuick.Dialogs 1.2
  5. Window
  6. {
  7. id:root
  8. objectName: "Houdini"
  9. visible: true
  10. width: 640
  11. height: 480
  12. title: qsTr("Hello World")
  13. color:"#202020"
  14. function loadTextToTextEdit(text)
  15. {
  16. textEdit.clear()
  17. var buffer = stream_01.slot_getBuffer()
  18. textEdit.append(buffer)
  19.  
  20. }
  21. function saveTextToDisk(file,buffer)
  22. {
  23. stream_01.slot_saveFile(file,buffer)
  24. }
  25.  
  26. Column
  27. {
  28. id:mainLayout
  29. padding: 5
  30. spacing: 10
  31. Row
  32. {
  33. id:buttonLayout
  34. spacing: 10
  35. Button
  36. {
  37. id:loadButton
  38. text:"load file"
  39. highlighted: true
  40. onClicked:
  41. {
  42. openDialog.open()
  43. }
  44. }
  45. Button
  46. {
  47. id:saveButton
  48. highlighted: true
  49. text:"save file"
  50. onClicked:
  51. {
  52. saveDialog.open()
  53. }
  54. }
  55.  
  56. }
  57. Rectangle
  58. {
  59. height: 1
  60. width: root.width
  61. id:menuRect
  62. color:"brown"
  63. }
  64.  
  65. Flickable
  66. {
  67. id:flick
  68. width: root.width; height: root.height;
  69. contentWidth: textEdit.paintedWidth
  70. contentHeight: textEdit.paintedHeight
  71. clip: true
  72. function ensureVisible(r)
  73. {
  74. if (contentX >= r.x)
  75. contentX = r.x;
  76. else if (contentX+width <= r.x+r.width)
  77. contentX = r.x+r.width-width;
  78. if (contentY >= r.y)
  79. contentY = r.y;
  80. else if (contentY+height <= r.y+r.height)
  81. contentY = r.y+r.height-height;
  82. }
  83. TextEdit
  84. {
  85. width: flick.width
  86. height: flick.height
  87. anchors.margins: 10
  88. focus: true
  89. id:textEdit
  90. text: ""
  91. color:"brown"
  92. font.family: "Helvetica"
  93. font.pointSize: 10
  94. font.bold: true
  95. cursorVisible: true
  96. selectByKeyboard: true
  97. selectByMouse: true
  98. wrapMode:TextEdit.WrapAnywhere
  99. onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
  100.  
  101. }
  102. }
  103.  
  104. }
  105. FileDialog
  106. {
  107. id:openDialog
  108. title: "Please choose a file"
  109. folder: shortcuts.home
  110. onAccepted:
  111. {
  112. console.log("You chose: " + openDialog.fileUrls)
  113. stream_01.slot_readFile(openDialog.fileUrls)
  114. var buffer = stream_01.slot_getBuffer()
  115. loadTextToTextEdit(buffer)
  116.  
  117. }
  118. onRejected:
  119. {
  120. console.log("Canceled")
  121. }
  122. }
  123. FileDialog
  124. {
  125. id:saveDialog
  126. title:"Save to a file"
  127. folder: shortcuts.home
  128. selectExisting : false
  129. onAccepted:
  130. {
  131. console.log("Save file : " + saveDialog.fileUrls)
  132. var text = textEdit.text;
  133. saveTextToDisk(saveDialog.fileUrl,text);
  134.  
  135. }
  136. onRejected:
  137. {
  138. console.log("Canceled")
  139. }
  140.  
  141. }
  142.  
  143. }

(2)

这次变化使用了QML里的connections.

可以调用C++里的signal,signal带参数也可以传递过来。注意查看新的main.qml

 代码区别就是读取用的C++信号读取的。

保存时用的信号在QML发射,然后调用C++的信号槽链接,来执行slot_saveFile()函数。

main.qml:

  1. import QtQuick 2.6
  2. import QtQuick.Window 2.2
  3. import QtQuick.Controls 2.1
  4. import QtQuick.Dialogs 1.2
  5. Window
  6. {
  7. id:root
  8. objectName: "Houdini"
  9. visible: true
  10. width:
  11. height:
  12. title: qsTr("Hello World")
  13. color:"#202020"
  14. function loadTextToTextEdit(text)
  15. {
  16. textEdit.clear()
  17. var buffer = stream_01.slot_getBuffer()
  18. textEdit.append(buffer)
  19.  
  20. }
  21. function saveTextToDisk(file,buffer)
  22. {
  23. stream_01.slot_saveFile(file,buffer)
  24. }
  25.  
  26. Column
  27. {
  28. id:mainLayout
  29. padding:
  30. spacing:
  31. Row
  32. {
  33. id:buttonLayout
  34. spacing:
  35. Button
  36. {
  37. id:loadButton
  38. text:"load file"
  39. highlighted: true
  40. onClicked:
  41. {
  42. openDialog.open()
  43. }
  44. }
  45. Button
  46. {
  47. id:saveButton
  48. highlighted: true
  49. text:"save file"
  50. onClicked:
  51. {
  52. saveDialog.open()
  53. }
  54. }
  55.  
  56. }
  57. Rectangle
  58. {
  59. height:
  60. width: root.width
  61. id:menuRect
  62. color:"brown"
  63. }
  64.  
  65. Flickable
  66. {
  67. id:flick
  68. width: root.width; height: root.height;
  69. contentWidth: textEdit.paintedWidth
  70. contentHeight: textEdit.paintedHeight
  71. clip: true
  72. function ensureVisible(r)
  73. {
  74. if (contentX >= r.x)
  75. contentX = r.x;
  76. else if (contentX+width <= r.x+r.width)
  77. contentX = r.x+r.width-width;
  78. if (contentY >= r.y)
  79. contentY = r.y;
  80. else if (contentY+height <= r.y+r.height)
  81. contentY = r.y+r.height-height;
  82. }
  83. TextEdit
  84. {
  85. width: flick.width
  86. height: flick.height
  87. anchors.margins:
  88. focus: true
  89. id:textEdit
  90. text: ""
  91. color:"brown"
  92. font.family: "Helvetica"
  93. font.pointSize:
  94. font.bold: true
  95. cursorVisible: true
  96. selectByKeyboard: true
  97. selectByMouse: true
  98. wrapMode:TextEdit.WrapAnywhere
  99. onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
  100.  
  101. }
  102. }
  103.  
  104. }
  105.  
  106. Connections
  107. {
  108. target: stream_01
  109. onSignal_readFile://当读取文件的时候回触发这个信号
  110. {
  111. var readText = buffer //buffer是signal_readFile(buffer)参数
  112. textEdit.clear()
  113. textEdit.append(readText)
  114. }
  115. }
  116.  
  117. // 读取文件的窗口
  118. FileDialog
  119. {
  120. id:openDialog
  121. title: "Please choose a file"
  122. folder: shortcuts.home
  123. onAccepted:
  124. {
  125. console.log("You chose: " + openDialog.fileUrl)
  126.  
  127. //这句话会触发signal_readFile信号
  128. stream_01.slot_readFile(openDialog.fileUrl)
  129. }
  130. onRejected:
  131. {
  132. console.log("Canceled")
  133. }
  134. }
  135. //保存文件窗口
  136. FileDialog
  137. {
  138. id:saveDialog
  139. title:"Save to a file"
  140. folder: shortcuts.home
  141. selectExisting : false
  142. onAccepted:
  143. {
  144.  
  145. console.log("Save file : " + saveDialog.fileUrl)
  146. var text = textEdit.text;
  147.  
  148. //保存触发信号,在C++中这个信号会触发保存
  149. stream_01.signal_saveFile(saveDialog.fileUrl,text)
  150.  
  151. }
  152. onRejected:
  153. {
  154. console.log("Canceled")
  155. }
  156.  
  157. }
  158.  
  159. }

(3)Q_PROPERTY宏,如果你想暴露一些member给QML对象。

  1. Q_OBJECT
  2.  
  3. Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
  4. public:
  5. QString message(){return _msg;}
  6. void setMessage(QString msg)
    {
    _msg = msg;
    emit messageChanged();
    }

类型如下:
Q_PROPERTY(任意类型 QML访问属性名 READ 读取函数名 WRITE 写的函数名  NOTIFY 信号触发)

<2>Q_INVOKABLE 宏,让QML可以随心所以调用函数。跟槽槽函数,信号一样调用。

<3>在C++修改QML对象的属性,从C++call javaScript

(1)修改qml root object的对象属性

  1. qDebug() <<engine.rootObjects()[]->objectName(); // this will be debug "Houdini"
  2. QObject *root_object = engine.rootObjects().value(); // houdini object ,it's the main object
  3.  
  4. // set QML object property
  5. //root_object->setProperty("x",600);
  6. QQmlProperty::write(root_object, "x", );
  7.  
  8. // read QML object property
  9. qDebug() << root_object->property("x").toInt();
  10. qDebug() << QQmlProperty::read(root_object,"x").toInt();
  11.  
  12. // read root object child by name
  13. //QObject *rect = root_object->findChild<QObject*>("rect");

(2) 假如qml root object 有个java函数:

  1. function javefunctest(arg)
  2. {
  3. console.log(arg);
  4. return "I'm jave script"
  5. }

C++访问:

  1. QObject *root_object = engine.rootObjects().value(); // houdini object ,it's the main object
  2. QVariant firstArg("I am C++ arg");
  3. QVariant retValue;
  4.  
  5. // call the jave script
  6. QMetaObject::invokeMethod(root_object,
  7. "javefunctest",
  8. Q_RETURN_ARG(QVariant,retValue),
  9. Q_ARG(QVariant,firstArg));
  10.  
  11. qDebug() << "ret value is " << retValue;

输出:

qml: I am C++ arg

ret value is QVariant(QString, "I'm java script")

<4> EMCA:

(1) 基本类型

  1. var flag =false //a boolean
  2.  
  3. var x =1,y=2
  4.  
  5. var str = 'abc' / "abc"
  6.  
  7. var test = {x:2,y:3}
  8.  
  9. console.log(test.x) //
  10.  
  11. console.log(test.y) //
  12.  
  13. test// object
  14.  
  15. (1) typeof()类型 关键字
  16.  
  17. To query the type of a variable, use the typeof keyword. typeof returns the name of the
  18. type as a string.
  19.  
  20. var x=1;
  21.  
  22. typeof(x) //"number"
  23.  
  24. typeof {x:1} //'object '
  25.  
  26. typeof typeof { x : 1 } // ’string’ 因为typeof()返回的是字符串.
  27.  
  28. (2) 转换类型
  29.  
  30. 1.3333333.toFixed(2) // ’1.33’
  31. 7..toString() // ’7’
  32.  
  33. (3) 可以显式的把boolean ,number,string转换成对象:
  34.  
  35. typeof 1. // ’number’
  36. typeof new Number(1.) // ’object’
  37. typeof new String(’Hi!’) // ’object’
  38.  
  39. 4
  40.  
  41. Objects themselves can be expressed using an array or object literal. Arrays have no separate
  42. type, but are specialized objects which use array indexes as properties:
  43. var o = { name: Werner’, age: 84 } // allocate simple object
  44. print(o.name, o[age])
  45. // both notations are valid, but [] notation allows generated strings
  46. var a = [’a’, b’, 7, 11.]
  47. // an array, equivalent to {’0’: ’a’, ’1’: ’b’, ’2’: 7, ’3’: 11.}
  48. typeof o, a // ’object’, ’object’

(2)函数

函数:所有的函数都会evaluates to something:

function f() {} //evaluates as 'undefined'

function f() {} +1 // evaluates as 1 ,because 'undefined' is casted to 0

(function f() {}) //evaluates to a function object

(function () {return 0;}) () /evaluates as 0

(3)

for loop :

i 作为了index

(4)

delete p.z // remove p.z

p.z //undefined

 (5)在列表中存入,或者在json对象中存在函数

(6) 创建对象new关键字

(1)

(2)创建对象默认构造函数:

这个时候Point其实就是个类。

为Point类添加个函数.

Each function in JavaScript can be used as a constructor in combination with the new operator.
To support inheritance, each function has a default property named prototype. Objects
created from a constructor inherit all properties from the constructor’s prototype. Consider the
following example:

其实 prototype里面的方法属于派生出来的,如何检查一个方法,或者一个类对象是原有的:

<2>公司一个小项目

按钮效果模仿的是Google material design风格。流动起来。参考上篇有详细代码.

V2:

3,CG Browser

New Version:0.00001

QML 从入门到放弃 第二卷的更多相关文章

  1. QML 从入门到放弃

    发现了一个问题: QQuickView only supports loading of root objects that derive from QQuickItem. If your examp ...

  2. Python从入门到放弃系列(Django/Flask/爬虫)

    第一篇 Django从入门到放弃 第二篇 Flask 第二篇 爬虫

  3. WPF从入门到放弃系列第二章 XAML

    本文是作者学习WPF从入门到放弃过程中的一些总结,主要内容都是对学习过程中拜读的文章的整理归纳. 参考资料 XAML 概述 (WPF):https://msdn.microsoft.com/zh-cn ...

  4. [精品书单] C#/.NET 学习之路——从入门到放弃

    C#/.NET 学习之路--从入门到放弃 此系列只包含 C#/CLR 学习,不包含应用框架(ASP.NET , WPF , WCF 等)及架构设计学习书籍和资料. C# 入门 <C# 本质论&g ...

  5. OpenStack从入门到放弃

    OpenStack从入门到放弃 目录: 为何选择云计算/云计算之前遇到的问题 什么是云计算 云服务模式 云应用形式 传统应用与云感知应用 openstack及其相关组件介绍 flat/vlan/gre ...

  6. 绕过校园网的共享限制 win10搭建VPN服务器实现--从入门到放弃

    一.开篇立论= =.. 上次说到博主在电脑上搭建了代理服务器来绕过天翼客户端的共享限制,然而经过实际测试还不够完美,所以本着生命不息,折腾不止的精神,我又开始研究搭建vpn服务器= =... (上次的 ...

  7. Android -- 带你从源码角度领悟Dagger2入门到放弃

    1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...

  8. Android -- 带你从源码角度领悟Dagger2入门到放弃(二)

    1,接着我们上一篇继续介绍,在上一篇我们介绍了简单的@Inject和@Component的结合使用,现在我们继续以老师和学生的例子,我们知道学生上课的时候都会有书籍来辅助听课,先来看看我们之前的Stu ...

  9. Android -- 带你从源码角度领悟Dagger2入门到放弃(一)

    1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...

随机推荐

  1. mac crontab调用python时出现ImportError: No module named XXX的问题

    写了一个监控mq的脚本,把这个脚本加入crontab里进行时刻监控,于是#crontab -e,添加语句: * * * * * cd /目录 && python mq脚本名.py &g ...

  2. Mysq基础l数据库管理、表管理、增删改数据整理

    一.       数据库管理: 创建数据库: create database(自定义) 查询所有数据库: show databases;(查询所有数据库) show create database ( ...

  3. gitlab ssh-key

    1.使用 ssh-keygen 生成一下ssh key 2. cat 对应路径 复制 ssh key到项目 settings --> deploy keys 添加 3. enable这个 key

  4. 持续集成CI

    一.CI 和 CD 持续集成是什么? 持续集成(Continuous integration,简称CI)指的是,频繁地(一天多次)将代码集成到主干.让产品可以快速迭代,同时还能保持高质量. 持续交付( ...

  5. hd loadBalanceServer F5 BIG-IP / Citrix NetScaler / Radware / Array / HAProxy /

    s 五.Citrix NetScaler 和 CDN 案例 问题描述: Citrix 10.5.66.9软件版本下,存在计时器bug,此bug会造成CDN长连接回源超过设备默认的180S,会发fin包 ...

  6. Nginx安装及配置详解包括windows环境

    nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  7. HDU 3371(城市联通 最小生成树-Kruskal)

    题意是求将所有点联通所花费的最小金额,如不能完全联通,输出 -1 直接Kruskal,本题带来的一点教训是 rank 是algorithm头文件里的,直接做变量名会导致编译错误.没查到 rank 的具 ...

  8. oldboy s21day05

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 1.请将列表中的每个元素通过 "_" 链接起来.'''users = ['李少奇','李 ...

  9. Java - 网络编程完全总结

    本文主要是自己在网络编程方面的学习总结,先主要介绍计算机网络方面的相关内容,包括计算机网络基础,OSI参考模型,TCP/IP协议簇,常见的网络协议等等,在此基础上,介绍Java中的网络编程. 一.概述 ...

  10. 访问权限,public private protected

    百度经验这篇文章很不错:https://jingyan.baidu.com/article/bad08e1e8e9a9b09c851219f.html