我有一個痛苦的問題,我浪費了很多時間。我需要幫助,因爲我沒有任何想法。無法在qml中創建C++類對象「無法將對象分配給列表屬性」數據「
這是它。我在Qt Creator中使用qml在Windows 8.1上編寫qt快速應用程序。創建了我自己的名爲「one」的C++類。通過註冊資格是:
在QML文件我進口它:
import OneClass 1.0
現在一切工作非常出色。但後來我決定創建共享庫,在那裏放置我的「一個」C++類。創建分離項目爲新建項目 - >庫 - > 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\5.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」:
我 「main.qml」 的文件,在這裏我聲明我的一類對象的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
}
海賊王:
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.h'的內容是什麼? –
@Patrizio,我在我的文章中加入了'one.h'的內容。 –
我們可以猜測什麼是One以及什麼是「資源」? – folibis