Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Trouble passing QList<QObject*> to QML
Forum Updated to NodeBB v4.3 + New Features

[Solved] Trouble passing QList<QObject*> to QML

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpmcbride7
    wrote on last edited by
    #1

    I am new to C++ and QT so if this problem is obvious, I apologize.

    I am having some issues passing a list of QObjects to QML. (Actually I am having trouble creating the QList in the first place).

    Here is a stripped version of my code that I am having trouble with:

    Interface.pro:
    @TEMPLATE = app

    QT += qml quick widgets sql network

    CONFIG += c++11

    SOURCES += main.cpp
    character.cpp
    cbobject.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 +=
    character.h
    cbobject.h
    @

    cbobject.h
    @#ifndef CBOBJECT_H
    #define CBOBJECT_H

    #include <QObject>
    #include <QString>
    #include <QList>

    class cbObject : public QObject
    {
    Q_OBJECT

    Q_PROPERTY(QString id READ id WRITE setid NOTIFY idChanged);
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged);
    Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged);
    

    public:
    cbObject(QObject *parent = 0);
    cbObject(const QString &id, const QString &name, const QString &color, QObject *parent=0);

    QString id();
    void setid(QString id);
    
    QString name();
    void setName(QString nme);
    
    QString color();
    void setColor(QString clr);
    

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

    public slots:

    private:
    QString m_id;
    QString m_name;
    QString m_color;

    };

    #endif // CBOBJECT_H
    @

    cbobject.cpp
    @#include "cbobject.h"

    QString cbObject::id(){
    return m_id;
    }

    void cbObject::setid(QString id){
    if (id != m_id) {
    m_id = id;
    emit idChanged();
    }
    }

    QString cbObject::name(){
    return m_name;
    }

    void cbObject::setName(QString nme){
    if (nme != m_name) {
    m_name = nme;
    emit nameChanged();
    }
    }

    QString cbObject::color(){
    return m_color;
    }

    void cbObject::setColor(QString clr){
    if (clr != m_color){
    m_color = clr;
    emit colorChanged();
    }
    }

    cbObject::cbObject(QObject *parent) :
    QObject(parent)
    {
    }
    @

    character.h
    @#ifndef CHARACTER_H
    #define CHARACTER_H

    #include <QStringList>
    #include <QSqlDatabase>
    #include <QSettings>
    #include <QNetworkReply>

    #include <QObject>

    class character : public QObject
    {
    Q_OBJECT

    public:
    explicit character(QObject *parent = 0);
    void getitemlist();

    QList<QObject*> ritem;
    

    signals:

    public slots:

    private:

    };

    #endif // CHARACTER_H
    @

    character.cpp
    @#include <QDebug>
    #include <QFileDialog>
    #include <QDir>
    #include <QStandardPaths>
    #include <QSqlQuery>
    #include <QSqlRecord>
    #include <QSqlError>
    #include <QSettings>
    #include <QFile>

    #include "character.h"
    #include "cbobject.h"

    #define MaxLogSize 100

    void character::getitemlist(){
    ritem.clear();
    ritem.append(new cbObject("1","Name1","red",this));
    ritem.append(new cbObject("2","Name2","black",this));
    }

    // Character file Functions

    character::character(QObject *parent) :
    QObject(parent)
    {
    }

    @

    and main.cpp:
    @#include <QGuiApplication>
    #include <QQmlEngine>
    #include <QQmlComponent>
    #include <QtCore>
    #include <QtGui>
    #include <QtQuick>

    #include "character.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    app.setApplicationDisplayName("PFCM");

    QUrl qmlSource("qrc:/main.qml");
    
    QQmlEngine *engine = new QQmlEngine();
    app.connect(engine, SIGNAL(quit()), SLOT(quit()));
    character ch;
    
    engine->rootContext()->setContextProperty("ch", &ch);
    engine->addImportPath("qrc:/other");
    
    QQmlComponent *component = new QQmlComponent(engine);
    
    component->loadUrl(qmlSource);
    if (!component->isReady())
    {
        qWarning("%s", qPrintable(component->errorString()));
        return -1;
    }
    
    QQuickItem *item = qobject_cast<QQuickItem *>(component->create());
    
    return app.exec&#40;&#41;;
    

    }
    @

    I am developing with QT 5.3.

    When I compile it I get the following compiler error:

    @Undefined symbols for architecture x86_64:
    "cbObject::cbObject(QString const&, QString const&, QString const&, QObject*)", referenced from:
    character::getitemlist() in character.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [Interface.app/Contents/MacOS/Interface] Error 1
    20:32:05: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project Interface (kit: Desktop Qt 5.3 clang 64bit)
    When executing step "Make"@

    I have been trying to use this as a guide:

    http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html#qobjectlist-based-model

    I am really struggling with this and I would appreciate any help.

    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpmcbride7
      wrote on last edited by
      #2

      Thanks!

      Just what I needed.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved