无法在 qml "Cannot assign object to list property " 数据中创建 C++ class 对象“”
Can't create C++ class object in qml "Cannot assign object to list property "data""
我有一个痛苦的问题,为此我浪费了很多时间。我需要帮助,因为我还没有任何想法。
在这里。我正在 Windows 8.1 上使用 Qt Creator 中的 qml 编写 qt 快速应用程序。创建了我自己的 C++ class,名称为“one”。通过以下方式注册:
qmlRegisterType<One>("OneClass", 1, 0, "One");
在我导入的qml文件中:
import OneClass 1.0
现在一切都很好。但后来我决定创建共享库,我把我的“一个”C++ class 放在那里。将单独的项目创建为新项目-> 库-> C++ 库。建立了名为“mainlib”的图书馆,一切都很好。通过在 .pro 文件中添加字符串将此库连接到我的应用程序:
DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib
运行 项目,那是我遇到这个问题的时刻:
QQmlApplicationEngine failed to load component
qrc:/main.qml:18 Cannot assign object to list property "data"
ASSERT: "!isEmpty()" in file C:\Program >Files\Qt.7\mingw53_32\include/QtCore/qlist.h, line 341
我的“one.h”文件的一部分:
#ifndef ONE_H
#define ONE_H
#include "mainlib_global.h"
#include <QObject>
class MAINLIBSHARED_EXPORT One : public QObject
{
Q_OBJECT
Q_PROPERTY(QString port_number READ get_port_number WRITE set_port_number)
Q_PROPERTY(QString port_speed READ get_port_speed WRITE set_port_speed)
Q_PROPERTY(QString x READ get_x WRITE set_x)
Q_PROPERTY(QString y READ get_y WRITE set_y)
public:
One();
~One();
QString get_port_number(void);
...
signals:
void portOpened(QString str);
...
private:
QString port_number;
QString x;
QString y;
};
#endif // ONE_H
"mainlib_global.h":
#ifndef MAINLIB_GLOBAL_H
#define MAINLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MAINLIB_LIBRARY)
# define MAINLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define MAINLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MAINLIB_GLOBAL_H
"mainlib.pro":
QT -= gui
TARGET = mainlib
TEMPLATE = lib
DEFINES += MAINLIB_LIBRARY
SOURCES += one.cpp
HEADERS += one.h\
mainlib_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
我的“main.qml”文件的一部分,我在其中声明了一个 class:
的对象
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.0
import OneClass 1.0
import QtQuick.Dialogs 1.1
Window {
id: mainWindow
visible: true
width: 1000
height: 720
minimumWidth: 930
color: "lightgrey";
property bool connected: false
// Object declaration
One {id: objOne}
...
}
我的“main.cpp”文件的一部分:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include "one.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QLocale::setDefault(QLocale::c());
qmlRegisterType<One>("OneClass", 1, 0, "One");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
...
return app.exec();
}
我的应用程序的 .pro 文件:
TEMPLATE = app
DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib
QT += qml quick serialport
CONFIG += c++11
win32: RC_ICONS += icon.ico
SOURCES += main.cpp \
#one.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
#one.h
如果取消注释“one.h”和“one.cpp”将它们与应用程序一起编译,则不会出现此问题并且一切正常。但是当评论它们与库一起工作时,我得到这个“无法将对象分配给列表属性“数据””的问题。
我尝试通过右键单击项目->添加库来连接库,但结果是一样的。我已阅读有关此“数据”属性 的文档,试图将对象声明显式分配给“数据”,但结果相同。尝试过:
resources: [
One {id: objOne}
]
并得到“无法将对象分配给列表 属性“资源””。我只是厌倦了解决这个问题。我几乎每一步都描述了你,因为我想也许我做错了什么?我求助...
一个可能的解决方案是声明一个新的 属性 并将其绑定到一个新的 'One' 组件:
Window {
property One objOne: One { }
}
BUT 如您所见,这可能会导致其他(线程)问题。不要这样做!
我强烈建议您使用Qt内置的插件机制。它旨在完全满足您的需求:导入外部动态 QML 库。
查看文档:http://doc.qt.io/qt-5/qtqml-modules-cppplugins.html
我有一个痛苦的问题,为此我浪费了很多时间。我需要帮助,因为我还没有任何想法。
在这里。我正在 Windows 8.1 上使用 Qt Creator 中的 qml 编写 qt 快速应用程序。创建了我自己的 C++ class,名称为“one”。通过以下方式注册:
qmlRegisterType<One>("OneClass", 1, 0, "One");
在我导入的qml文件中:
import OneClass 1.0
现在一切都很好。但后来我决定创建共享库,我把我的“一个”C++ class 放在那里。将单独的项目创建为新项目-> 库-> C++ 库。建立了名为“mainlib”的图书馆,一切都很好。通过在 .pro 文件中添加字符串将此库连接到我的应用程序:
DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib
运行 项目,那是我遇到这个问题的时刻:
QQmlApplicationEngine failed to load component
qrc:/main.qml:18 Cannot assign object to list property "data"
ASSERT: "!isEmpty()" in file C:\Program >Files\Qt.7\mingw53_32\include/QtCore/qlist.h, line 341
我的“one.h”文件的一部分:
#ifndef ONE_H
#define ONE_H
#include "mainlib_global.h"
#include <QObject>
class MAINLIBSHARED_EXPORT One : public QObject
{
Q_OBJECT
Q_PROPERTY(QString port_number READ get_port_number WRITE set_port_number)
Q_PROPERTY(QString port_speed READ get_port_speed WRITE set_port_speed)
Q_PROPERTY(QString x READ get_x WRITE set_x)
Q_PROPERTY(QString y READ get_y WRITE set_y)
public:
One();
~One();
QString get_port_number(void);
...
signals:
void portOpened(QString str);
...
private:
QString port_number;
QString x;
QString y;
};
#endif // ONE_H
"mainlib_global.h":
#ifndef MAINLIB_GLOBAL_H
#define MAINLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MAINLIB_LIBRARY)
# define MAINLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define MAINLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MAINLIB_GLOBAL_H
"mainlib.pro":
QT -= gui
TARGET = mainlib
TEMPLATE = lib
DEFINES += MAINLIB_LIBRARY
SOURCES += one.cpp
HEADERS += one.h\
mainlib_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
我的“main.qml”文件的一部分,我在其中声明了一个 class:
的对象import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.0
import OneClass 1.0
import QtQuick.Dialogs 1.1
Window {
id: mainWindow
visible: true
width: 1000
height: 720
minimumWidth: 930
color: "lightgrey";
property bool connected: false
// Object declaration
One {id: objOne}
...
}
我的“main.cpp”文件的一部分:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include "one.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QLocale::setDefault(QLocale::c());
qmlRegisterType<One>("OneClass", 1, 0, "One");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
...
return app.exec();
}
我的应用程序的 .pro 文件:
TEMPLATE = app
DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib
QT += qml quick serialport
CONFIG += c++11
win32: RC_ICONS += icon.ico
SOURCES += main.cpp \
#one.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
#one.h
如果取消注释“one.h”和“one.cpp”将它们与应用程序一起编译,则不会出现此问题并且一切正常。但是当评论它们与库一起工作时,我得到这个“无法将对象分配给列表属性“数据””的问题。
我尝试通过右键单击项目->添加库来连接库,但结果是一样的。我已阅读有关此“数据”属性 的文档,试图将对象声明显式分配给“数据”,但结果相同。尝试过:
resources: [
One {id: objOne}
]
并得到“无法将对象分配给列表 属性“资源””。我只是厌倦了解决这个问题。我几乎每一步都描述了你,因为我想也许我做错了什么?我求助...
一个可能的解决方案是声明一个新的 属性 并将其绑定到一个新的 'One' 组件:
Window {
property One objOne: One { }
}
BUT 如您所见,这可能会导致其他(线程)问题。不要这样做!
我强烈建议您使用Qt内置的插件机制。它旨在完全满足您的需求:导入外部动态 QML 库。 查看文档:http://doc.qt.io/qt-5/qtqml-modules-cppplugins.html