2016-02-12 58 views
0

是否有可能將QList<T>從C++公開到QML,其中T類型被元對象系統知道?C++/QML序列數據交換

//C++: 
class Data : public QObject { 
    Q_OBJECT 
    Q_PROPERTY(QList<QGeoCoordinate> path READ path WRITE setPath NOTIFY pathChanged) 

數據實例作爲上下文屬性公開爲QML,但在QML側的路徑屬性未定義。 在文檔寫入

特別地,QML目前支持:

QList<int> 
QList<qreal> 
QList<bool> 
QList<QString> and QStringList 
QList<QUrl> 

其他序列類型不透明支撐,而不是任何其他序列類型的 實例將QML和C++ 之間傳遞作爲一個不透明的QVariantList。

但看起來像不透明轉換爲QVariantList不起作用。 我正在使用Qt 5.6 RC快照。

回答

1

我不確定轉換爲QVariantList時應該發生什麼。我不明白你怎麼能夠訪問列表的內容,因爲包含的類型不是QObject。我認爲那裏的文件可以更清楚。

不過,您可以改用QQmlListPropertyExtending QML using Qt C++有關於其使用的更多信息。下面是directory.cpp從例子的源代碼:

/* 
    Function to append data into list property 
*/ 
void appendFiles(QQmlListProperty<File> *property, File *file) 
{ 
    Q_UNUSED(property) 
    Q_UNUSED(file) 
    // Do nothing. can't add to a directory using this method 
} 

/* 
    Function called to retrieve file in the list using an index 
*/ 
File* fileAt(QQmlListProperty<File> *property, int index) 
{ 
    return static_cast< QList<File *> *>(property->data)->at(index); 
} 

/* 
    Returns the number of files in the list 
*/ 
int filesSize(QQmlListProperty<File> *property) 
{ 
    return static_cast< QList<File *> *>(property->data)->size(); 
} 

/* 
    Function called to empty the list property contents 
*/ 
void clearFilesPtr(QQmlListProperty<File> *property) 
{ 
    return static_cast< QList<File *> *>(property->data)->clear(); 
} 

/* 
    Returns the list of files as a QQmlListProperty. 
*/ 
QQmlListProperty<File> Directory::files() 
{ 
    refresh(); 
    return QQmlListProperty<File>(this, &m_fileList, &appendFiles, &filesSize, &fileAt, &clearFilesPtr); 
} 

你也可以做的露出QList of QObject-derived types類似的東西,但有自己的回退。