I have a painful problem, that I wasted a lot of time for. I need help, because I don't have any idea already.
Here it is. I'm writing qt quick application using qml in Qt Creator on Windows 8.1. Created my own C++ class with name "one". Registrated it via:
qmlRegisterType<One>("OneClass", 1, 0, "One");
In qml file I imported it:
import OneClass 1.0
By now everything worked very well. But then I decided to create shared library, where I put my "One" C++ class. Created separated project as New project->Library->C++ Library. Built library with name "mainlib", everything was fine. Connected this library to my application by adding strings in .pro file:
DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib
Run project and that's moment when I've got this problem:
QQmlApplicationEngine failed to load component
qrc:/main.qml:18 Cannot assign object to list property "data"
ASSERT: "!isEmpty()" in file C:\Program >Files\Qt\5.7\mingw53_32\include/QtCore/qlist.h, line 341
Piece of my "one.h" file:
#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
}
Piece of my "main.qml" file, where I declared my object of One 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}
...
}
Piece of my "main.cpp" file:
#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();
}
My application's .pro file:
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
Here is the thing if uncomment "one.h" and "one.cpp" to compile them alongside with application, this problem don't appear and everything works fine. But when comment them to work with library, I get this "Cannot assign object to list property "data"" problem.
I've tried to connect library via right click on project->Add Library, but result is the same. I've read documentation about this "data" property, tried to explicitly assign object declaration to "data", but got the same. Tried:
resources: [
One {id: objOne}
]
And got "cannot assign object to list property "resources"". I'm just exhausted with solving this problem. I described you almost every my step, because I think that maybe I do something in wrong way? I'm begging for help...
one.h? - Patrizio Bekerleone.hto my post. - Hermanresourcerelates to? - folibisOneis my C++ class, that I registrated as qml type. I thought according to the stringsqmlRegisterType<One>("OneClass", 1, 0, "One");andimport OneClass 1.0, it should be obvious. I tried to assignobjOnetoresourceseverywhere, where it was possible. - Hermanresource, notOne's declaration. But you didn't specify what is it and to what object it related. - folibis