Embedding a shared library in a QML File



  • I'm trying to use a shared library (created by me) in a QML file. The project its about a game with virtual players. Each 'virtual' player it's just a shared library that exposes a name and a playing function. All seems to be good until the compiling phase when the next error arises:
    C:\Qt\Qt5.3.0\5.3\mingw482_32\include\QtCore\qmetatype.h:1297: error: 'QObject' is an inaccessible base of 'Player'
    enum { Value = sizeof(checkType(static_cast<T*>(0))) == sizeof(yes_type) };
    ^

    this is the .pro file:
    TEMPLATE = app
    QT += qml quick widgets
    SOURCES += main.cpp
    RESOURCES += qml.qrc
    resources.qrc
    QML_IMPORT_PATH =
    include(deployment.pri)
    OTHER_FILES +=
    unix|win32: LIBS += -L$$PWD/dlls/ -lplayer
    INCLUDEPATH += $$PWD/dlls
    DEPENDPATH += $$PWD/dlls

    the player.h file:
    #ifndef PLAYER_H
    #define PLAYER_H

    #include "player_global.h"
    #include <QObject>
    #include <QString>

    class PLAYERSHARED_EXPORT Player : QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged)
    QString name;
    Q_PROPERTY(QString id READ getId WRITE setId NOTIFY idChanged)
    QString id;

    public:
    Player(QString name);
    const QString getName();
    void setName(const QString name);
    const QString getId();
    void setId(const QString id);

    signals:
    void nameChanged();
    void idChanged();
    };

    #endif // PLAYER_H

    the player.cpp file:
    #include "player.h"

    Player::Player(QString name = "")
    {
    this->name = name;
    }

    const QString Player::getName()
    {
    return this->name;
    }

    void Player::setName(const QString name)
    {
    this->name = name;
    }

    const QString Player::getId()
    {
    return this->id;
    }

    void Player::setId(const QString id)
    {
    this->id = id;
    }

    the main.cpp:
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml>

    #include "player.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    qmlRegisterType<Player>("cu.uci.fac5.player", 1, 0, "Player");
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
    return app.exec();
    

    }


  • Moderators

    Hi @Reinier-R.-Gonzalez,

    The class should be declared as:

    class PLAYERSHARED_EXPORT Player : public QObject //Note the public keyword
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.