Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

print c++ data in qml list view



  • i want to print c++ custom data class in qml list view by delegate.

    should i make model in c++ and then call it in qml?

    or is there any way call only data in qml??


  • Lifetime Qt Champion

    Hi,

    What kind of data are you talking about ?



  • class resource: public QObject
    {
    Q_OBJECT
    
    public:
        resource(QString);
        ~resource();
        Q_INVOKABLE void addLoss(float, float);
        Q_INVOKABLE float getLoss();
        Q_INVOKABLE QString getName();
    
    private:
        QString name;
        QList<float> *Loss;
    };
    

    my data class is above. and i made resource list as bleow

    in main.cpp

        QList<resource*> *resourceArr;
        qDebug()<< "declare resourceArr";
        resourceArr = new QList<resource*>;
        resource *a = new resource("name");
        resourceArr->push_back(a);
    
        QQmlApplicationEngine engine;
        QQmlContext *context = new QQmlContext(engine.rootContext());   // c++ class to qml object
        context->setContextProperty("resourceVec", &resourceArr);
    

    last line gives me an error like "calling a private constructor of class 'QVariant'"
    and also "atemp to use a deleted function"

    what i want is print resourceArray[idx].getName() / resourceArray[idx].getLoss() in my listView.

    problem looks simple but i have a hard time with it..... i'm noob in qt.


  • Lifetime Qt Champion

    You are passing a pointer to the pointer of your QList object. That's wrong.



  • i Solved this!. it was not matter of pointers, i've misunderstood about QObect.
    thanks


Log in to reply