访问QML中的QList对象
Access QList object in QML
自从Javascript以来,我在访问QList对象时遇到了一个小问题。我有一个C++类,它允许我从QML/JS开始执行SQL查询。一切正常,我在C++中得到了结果。
我的问题是,我向QML返回了一个QList对象。这是我在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;
}
但是当我从JS调用这个函数时,我得到了这个错误:Unknown method return type: QList<Note>
问题是返回类型,QML JS不知道类型QList<Object>
,为什么?我做错了什么
如果您想在Qml中使用c++QList
作为模型,我建议您使用以下过程。我用我自己的例子,你可以根据自己的需要进行更改。
存储.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);
};
存储.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
int main(int argc, char *argv[])
{
qmlRegisterType<Note>();
}
QQmlListProperty
类允许应用程序向QML
公开类似列表的属性。要提供列表属性,C++类必须实现操作回调,然后从属性getter返回适当的QQmlListProperty
值。列表属性不应具有setter。当用C++代码扩展QML
时,可以向QML
类型系统注册C++类,以使该类能够用作QML
代码中的数据类型。
您是否将Note注册为元类型?也许这就是缺少的:
http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE
在Qt 4中,您还必须注册QList<Note>
,但在Qt 5中不需要。
哦,Note可能应该是Q_GADGET
,否则您也无法从QML访问其内容。请确保使用Qt 5.5+。否则,您将需要QList<Note*>
并使这些Note
对象继承自QObject
。
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 如何在 c++98 中通过特定字段在 QList 中查找对象?
- 如何将对象/指针正确存储到 Qlist 中
- 如何在循环中初始化 QList 中的对象
- 当删除上下文属性中的QLIST对象时,QT QML应用程序崩溃
- 将QPAIR的Qlist写入QSETTINGS对象
- 通过QLIST迭代并在包含对象上调用函数
- QML/QT 无法将 C++ 中的 QList<QVariantMap> 转换为 JavaScript 中的对象数组
- Qt QList不附加本地对象
- 访问QML中的QList对象
- 在c++ /QT中创建抽象类对象的QList
- 如何将对象附加到 QList
- 设置QList子对象
- 使用setParent()设置QList为父对象
- QList clear函数调用是否清除存储在QList中的动态分配对象的内存?
- 用于跟踪兄弟对象的QObject指针的静态QList
- 从QList中删除重复对象
- 获取QList对象的ListView索引
- 将局部声明的对象存储在全局QList中是否安全?