首页 文章

无法在qml中创建C类对象“无法将对象分配给列表属性”数据“”

提问于
浏览
3

我有一个痛苦的问题,我浪费了很多时间 . 我需要帮助,因为我已经不知道了 .

这里是 . 我在Windows 8.1的Qt Creator中使用qml编写qt快速应用程序 . 创建了我自己的名为“one”的C类 . 通过以下方式注册:

qmlRegisterType<One>("OneClass", 1, 0, "One");

在qml文件中我导入了它:

import OneClass 1.0

到现在为止一切都很顺利 . 但后来我决定创建共享库,在那里我把我的“One”C类 . 将创建的分隔项目创建为New project-> Library-> C Library . Build 了名为“mainlib”的库,一切都很好 . 通过在.pro文件中添加字符串将此库连接到我的应用程序:

DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib

运行项目,那是我遇到这个问题的时刻:

QQmlApplicationEngine无法加载组件qrc:/main.qml:18无法将对象分配给列表属性“data”ASSERT:“!isEmpty()”在文件C:\ Program> Files \ Qt \ 5.7 \ mingw53_32 \ include / QtCore / qlist.h,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”文件的一部分,我在其中声明了我的One类对象:

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}
]

并得到“无法将对象分配给列表属性”资源“” . 我只是厌倦了解决这个问题 . 我几乎每一步都描述了你,因为我认为也许我做错了什么?我求救...

1 回答

  • 2

    一种可能的解决方案是声明一个新属性并将其绑定到一个新的“One”组件:

    Window {
        property One objOne: One { }
    }
    

    BUT 因为您会看到这可能会导致其他(线程)问题 . 不要这样做!

    strongly 建议你使用Qt内置插件机制 . 它旨在完全按照您的意愿执行:导入外部动态QML库 . 查看文档:http://doc.qt.io/qt-5/qtqml-modules-cppplugins.html

相关问题