本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873)
又是一個新的系列了,不過這個系列和我們之前的Chapter系列是及其相似的,但是不過呢,Chapter主要演示了如何使用C++創建具有可視性的類型以擴展我們的QML,而這個系列則關注于如何使用C++擴展QML非可視化的內容。
這里第一個小例子與Chapter的第一個小例子及其類似:

person是我們自定義的C++類,然后我們將其注冊為QML類型供資源文件中的example.qml使用。
person.h,這個類與之前的piechart沒有太大區別:
~~~
#ifndef PERSON_H
#define PERSON_H
#include <QObject>
//![0]
class Person : public QObject // 要注意的是由于這個對象并不需要可視化,我們繼承最基礎的QObject就可以了
{
Q_OBJECT // 因為QML組件基于元對象系統,所以QObject和Q_OBJECT都不能少
Q_PROPERTY(QString name READ name WRITE setName) // 兩個自定義屬性
Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
public:
Person(QObject *parent = 0);
QString name() const;
void setName(const QString &);
int shoeSize() const;
void setShoeSize(int);
private:
QString m_name;
int m_shoeSize;
};
//![0]
~~~
person.cpp:
~~~
#include "person.h"
// ![0]
Person::Person(QObject *parent)
: QObject(parent), m_shoeSize(0)
{
}
QString Person::name() const
{
return m_name;
}
void Person::setName(const QString &n)
{
m_name = n;
}
int Person::shoeSize() const
{
return m_shoeSize;
}
void Person::setShoeSize(int s)
{
m_shoeSize = s;
}
~~~
example.qml:
~~~
import People 1.0
Person { // 非可視化組件,我們也不再需要以Item作為父對象
name: "Bob Jones"
shoeSize: 12
}
~~~
main.cpp:
~~~
#include <QCoreApplication> // 注意到Chapter中為QGuiApplication
#include <QQmlEngine> // 提供QML組件的運行環境
#include <QQmlComponent> // 提供對QML組件的封裝與訪問
#include <QDebug>
#include "person.h"
int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv);
//![0]
qmlRegisterType<Person>("People", 1,0, "Person"); // 注冊QML類型
//![0]
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:example.qml")); // 獲取QML文件中的組件
Person *person = qobject_cast<Person *>(component.create()); // 創建該組件的實例化對象
if (person) {
qWarning() << "The person's name is" << person->name(); // 依然是通過->訪問其成員函數
qWarning() << "They wear a" << person->shoeSize() << "sized shoe";
} else {
qWarning() << component.errors(); // 類型轉換失敗則輸出錯誤信息
}
return 0;
}
~~~
由于沒有界面,實際運行就是一行控制臺的輸出:

- 前言
- 1——Fortune Server/Client
- 2——Multicast Sender/Receiverz
- 3——Broadcast Sender/Receiver
- 4——Blocking Fortune Client
- 5——Threaded Fortune Server
- 5(總結)——Fortune例程的各個實現區別
- 6——Loopback Example
- 7——Analog Clock Example
- 8——Shaped Clock Example
- 9——Analog Clock Window Example
- 10——Qt Quick Particles Examples - Emitters
- 11——Qt Quick Particles Examples - Affectors
- 12——Qt Quick Particles Examples - CustomParticles
- 13——Qt Quick Particles Examples - Image Particles
- 14——Qt Quick Particles Examples - System
- 15——Chapter 1: Creating a New Type
- 16——Chapter 2: Connecting to C++ Methods and Signals
- 17——Chapter 3: Adding Property Bindings
- 18——Chapter 4: Using Custom Property Types
- 19——Chapter 5: Using List Property Types
- 20——Chapter 6: Writing an Extension Plugin
- 21——Extending QML - Adding Types Example
- 22——Extending QML - Object and List Property Types Example
- 23——Extending QML - Inheritance and Coercion Example
- 24——Extending QML - Default Property Example
- 25——Extending QML - Methods Example
- 26——Extending QML - Grouped Properties Example
- 27——Extending QML - Attached Properties Example
- 28——Extending QML - Signal Support Example
- 29——Extending QML - Property Value Source Example
- 30——Extending QML - Binding Example
- 31——StocQt
- 32——Qt Quick Examples - Threading
- 33——Qt Quick Examples - Window and Screen
- 34——Concentric Circles Example
- 35——Music Player
- 36——Wiggly Example
- 37——Vector Deformation