QML和C ++的結合
2018/11/7
黃雲璘
QML和C ++的結合
• 來源:
– Overview - QML and C++ Integration
• QML被設計成可經由C ++ code輕鬆擴展
• 藉由混合使用QML,JavaScript和C ++ code
來實現
• 將使用者介面code與應用程序邏輯code分開,
在QML文件中使用QML和JavaScript實現前者,
後者使用C ++實現
QML和C ++的結合
• 要為QML提供某些C ++數據(data)或功能
(functionality),必須從源於QObject 的類別去實現
• 任何源於QObject之類別的屬性(properties),方法
(methods)和信號(signals)都可以從QML訪問
• 最常見方法:將類別—
– 註冊為可實例化的QML類型(instantiable QML type),因
此可以像QML code中的任何普通QML object type一樣進
行實例化(instance)和使用
– 註冊為單例類型(Singleton Type),以便可以從QML code
導入(import)該類別的單個實例(single instance),從而允
許從QML訪問實例的屬性,方法和信號
– 其類別的實例(instance) 作為上下文屬性(context
property) 或上下文對象(context object) 鑲嵌(embedded)
到QML代碼中,從而允許從QML訪問實例的屬性,方法
和信號
QML和C ++的結合
• Qt QML modules亦提供了從C ++ code執行反
向和操作QML物件的方法
– Interacting with QML Objects from C++
• C ++ code可以結合到C ++應用程序
(application)或C ++插件(plugin)中
– 在C ++插件中提供類型和功能(Providing Types
and Functionality in a C++ Plugin)
QML和C ++的結合 -
註冊 Registering
• 可實例化類型 Registering an instantiable type
• the class can be declared and instantiated like any other object type
from QML code
• 不可實例化類型 Registering a non-instantiable type
• Sometimes a QObject-derived class may need to be registered with
the QML type system but not as an instantiable type
– qmlRegisterType() (with no parameters) : base class type
– qmlRegisterInterface() : 註冊為interface type
– qmlRegisterUncreatableType()
– qmlRegisterSingletonType()
QML和C ++的結合 -
鑲嵌 Embed by Context Property
• Inject(注入) C++ data into a QML object is
made possible by the QQmlContext class
QML和C ++的結合 - 如何選擇?
http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html
QML和C ++的結合 -
QML和C ++之間的Data type轉換
https://doc.qt.io/qt-5.11/qtqml-cppintegration-data.html

Overview - QML and C++ Integration

  • 1.
  • 2.
    QML和C ++的結合 • 來源: –Overview - QML and C++ Integration • QML被設計成可經由C ++ code輕鬆擴展 • 藉由混合使用QML,JavaScript和C ++ code 來實現 • 將使用者介面code與應用程序邏輯code分開, 在QML文件中使用QML和JavaScript實現前者, 後者使用C ++實現
  • 3.
    QML和C ++的結合 • 要為QML提供某些C++數據(data)或功能 (functionality),必須從源於QObject 的類別去實現 • 任何源於QObject之類別的屬性(properties),方法 (methods)和信號(signals)都可以從QML訪問 • 最常見方法:將類別— – 註冊為可實例化的QML類型(instantiable QML type),因 此可以像QML code中的任何普通QML object type一樣進 行實例化(instance)和使用 – 註冊為單例類型(Singleton Type),以便可以從QML code 導入(import)該類別的單個實例(single instance),從而允 許從QML訪問實例的屬性,方法和信號 – 其類別的實例(instance) 作為上下文屬性(context property) 或上下文對象(context object) 鑲嵌(embedded) 到QML代碼中,從而允許從QML訪問實例的屬性,方法 和信號
  • 4.
    QML和C ++的結合 • QtQML modules亦提供了從C ++ code執行反 向和操作QML物件的方法 – Interacting with QML Objects from C++ • C ++ code可以結合到C ++應用程序 (application)或C ++插件(plugin)中 – 在C ++插件中提供類型和功能(Providing Types and Functionality in a C++ Plugin)
  • 5.
    QML和C ++的結合 - 註冊Registering • 可實例化類型 Registering an instantiable type • the class can be declared and instantiated like any other object type from QML code • 不可實例化類型 Registering a non-instantiable type • Sometimes a QObject-derived class may need to be registered with the QML type system but not as an instantiable type – qmlRegisterType() (with no parameters) : base class type – qmlRegisterInterface() : 註冊為interface type – qmlRegisterUncreatableType() – qmlRegisterSingletonType()
  • 6.
    QML和C ++的結合 - 鑲嵌Embed by Context Property • Inject(注入) C++ data into a QML object is made possible by the QQmlContext class
  • 7.
    QML和C ++的結合 -如何選擇? http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html
  • 8.
    QML和C ++的結合 - QML和C++之間的Data type轉換 https://doc.qt.io/qt-5.11/qtqml-cppintegration-data.html