[英]Access QList object in QML
I have a little problem accessing a QList objects since Javascript. 自Javascript以来我访问QList对象有点问题。 I have a C ++ class that allows me to perform SQL queries since QML / JS.
我有一个C ++类,允许我从QML / JS开始执行SQL查询。 Everything works, I get my results in C ++.
一切正常,我用C ++得到了我的结果。
My problem is that I'd returned to QML an QList object. 我的问题是我向QML返回了一个QList对象。 This is my function in C++ to return SQL result (Note is a simple object with different attributes) :
这是我在C ++中返回SQL结果的函数(注意是一个具有不同属性的简单对象):
QList<Note> Storage::setQuery(QString query)
{
QList<Note> noteItems;
QSqlQuery qsqlQuery;
bool ok = qsqlQuery.exec(query);
if(!ok)
{
qDebug() << "Error setQuery" << m_sqlDatabase.lastError();
}
else
{
while (qsqlQuery.next()) {
Note my_note;
QString note = qsqlQuery.value("message").toString();
my_note.setMessage(note);
noteItems.append(my_note);
}
}
return noteItems;
}
But when I call this function from JS I get this error: Unknown method return type: QList<Note>
The problem is the return type, QML JS doesn't know the type QList<Object>
, why? 但是当我从JS调用这个函数时我得到了这个错误:
Unknown method return type: QList<Note>
问题是返回类型,QML JS不知道类型QList<Object>
,为什么? What do I do wrong 我做错了什么
If you want to use c++ QList
as a model in Qml, I would recommend you to use the following procedure. 如果你想在Qml中使用c ++
QList
作为模型,我建议你使用以下过程。 I'm using my own example, you might change it according to your needs. 我正在使用我自己的例子,你可以根据自己的需要改变它。
Storage.h storage.h定义
class Storage : public QObject {
Q_PROPERTY(QQmlListProperty<Note> getList READ getList)
public:
QQmlListProperty<Note> getList();
void setQuery(QString query);
QList<Note> noteItems;;
private:
static void appendList(QQmlListProperty<Note> *property, Note *note);
static Note* cardAt(QQmlListProperty<Note> *property, int index);
static int listSize(QQmlListProperty<Note> *property);
static void clearListPtr(QQmlListProperty<Note> *property);
};
Storage.cpp Storage.cpp
void Field::appendList(QQmlListProperty<Card> *property, Note *note) {
Q_UNUSED(property);
Q_UNUSED(note);
}
Note* Field::cardAt(QQmlListProperty<Note> *property, int index) {
return static_cast< QList<Note> *>(property->data)->at(index);
}
int Field::listSize(QQmlListProperty<Note> *property) {
return static_cast< QList<Note> *>(property->data)->size();
}
void Field::clearListPtr(QQmlListProperty<Note> *property) {
return static_cast< QList<Note> *>(property->data)->clear();
}
QQmlListProperty<Note> Field::getList() {
return QQmlListProperty<Note>( this, &list[0], &appendList, &listSize, &cardAt, &clearListPtr );
}
void Storage::setQuery(QString query)
{
QList<Note> noteItems;
QSqlQuery qsqlQuery;
bool ok = qsqlQuery.exec(query);
if(!ok)
{
qDebug() << "Error setQuery" << m_sqlDatabase.lastError();
}
else
{
while (qsqlQuery.next()) {
Note my_note;
QString note = qsqlQuery.value("message").toString();
my_note.setMessage(note);
noteItems.append(my_note);
}
}
}
main.cpp main.cpp中
int main(int argc, char *argv[])
{
qmlRegisterType<Note>();
}
The QQmlListProperty
class allows applications to expose list-like properties to QML
. QQmlListProperty
类允许应用程序向QML
公开类似列表的属性。 To provide a list property, a C++ class must implement the operation callbacks, and then return an appropriate QQmlListProperty
value from the property getter. 要提供list属性,C ++类必须实现操作回调,然后从属性getter返回适当的
QQmlListProperty
值。 List properties should have no setter. 列表属性应该没有setter。 When extending
QML
with C++ code, a C++ class can be registered with the QML
type system to enable the class to be used as a data type within QML
code. 当使用C ++代码扩展
QML
时,可以向QML
类型系统注册C ++类,以使该类能够用作QML
代码中的数据类型。
Did you register Note as a meta type? 您是否将Note注册为元类型? Probably that's what's missing:
可能那就是缺少的东西:
http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE
In Qt 4 you'll also have to register QList<Note>
, but not in Qt 5. 在Qt 4中,您还必须注册
QList<Note>
,但不能在Qt 5中注册。
Oh, and Note should probably be a Q_GADGET
, otherwise you cannot access its contents from QML either. 哦,Note应该是
Q_GADGET
,否则你也无法从QML访问它的内容。 Make sure you use Qt 5.5+. 确保使用Qt 5.5+。 Otherwise, you'll need
QList<Note*>
and make those Note
objects inherit from QObject
. 否则,您将需要
QList<Note*>
并使这些Note
对象继承自QObject
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.