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

Not able to pass data from C++ to QML



  • Hi everybody.
    I'm not able to pass some objects list to QML and I don't understand where is the problem.
    Windows + Qt 5.7.1
    I defined a simple object:

    mymedia.cpp:

    #include "mymedia.h"
    
    MyMedia::MyMedia(QString path, int duration) : m_path(path), m_duration(duration)
    {
    }
    
    QString MyMedia::path() const {
        return m_path;
    }
    
    int MyMedia::duration() const {
        return m_duration;
    }
    
    

    mymedia.h:

    #ifndef MYMEDIA_H
    #define MYMEDIA_H
    
    #include <QObject>
    
    class MyMedia : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QString path READ path) 
        Q_PROPERTY(int duration READ duration)
    
    public:
        MyMedia(QString path, int duration = 0);
    
        QString path() const;
        int duration() const;
    
    private:
        QString m_path;
        int m_duration;
    
    };
    
    #endif // MYMEDIA_H
    
    

    main.cpp:

    QList<QObject*> contentList;
    ...
    ...
    context->setContextProperty("fileDataModel", QVariant::fromValue(contentList));
    
    

    main.qml:

     ListView {
                id: listView
                x: 522
                y: 8
                width: 110
                height: 160
                model: fileDataModel
                delegate: Item {
                    id: itemRowFile
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        id: row1
                        Rectangle {
                            width: 40
                            height: 40
                            // color: colorCode
                        }
    
                        Text {
                            text: "Q "+modelData.path
                            anchors.verticalCenter: parent.verticalCenter
                            font.bold: true
                        }
                        spacing: 10
                    }
                }
            }
    

    I checked that contentList is filled with my data, but the list on the screen is always empty.
    Where I'm wrong?

    Regards



  • Hope I finally solved ...
    The problem was the line

    property var fileList
    

    in the qml file that overwrites the one (same name) passed from the C++ code.



  • @SteMMo said in Not able to pass data from C++ to QML:

    context->setContextProperty("fileDataModel", QVariant::fromValue(contentList));

    I've never seen that QVariant::fromValue(contentList) being used, does

    context->setContextProperty("fileDataModel", &contentList);
    

    not work?



  • I solved.
    I think the problem was the id of the Row element inside the Item delegate.



  • Sorry, this is not true :(

    The truth is that I can see the data if I pass them as ListView model, but I cannot see them if I pass them in a var properties.

    Window {
    
        property var fileList
    
        onFileListChanged: {
            console.log("Lista:")
            for(var property in fileList)
                console.log(property)
            console.log("fine Lista")
        }
    
    

    In the main source:

        context->setContextProperty("fileList", QVariant::fromValue(contentList));  // Not passed
    
        context->setContextProperty("fileDataModel", QVariant::fromValue(contentList));  // ok passed
    
    


  • @Xyrer If I change the code as you suggest:

        context->setContextProperty("fileList", &contentList); // QVariant::fromValue(contentList));  // ?
    
    

    I have some errors:

    In file included from C:\Qt\Qt5.7.1_MinGw\5.7\mingw53_32\include/QtCore/qlocale.h:43:0,
                     from C:\Qt\Qt5.7.1_MinGw\5.7\mingw53_32\include\QtGui/qguiapplication.h:46,
                     from C:\Qt\Qt5.7.1_MinGw\5.7\mingw53_32\include\QtGui/QGuiApplication:1,
                     from ..\QtComuneQml\main.cpp:9:
    C:\Qt\Qt5.7.1_MinGw\5.7\mingw53_32\include/QtCore/qvariant.h: In function 'int qMain(int, char**)':
    C:\Qt\Qt5.7.1_MinGw\5.7\mingw53_32\include/QtCore/qvariant.h:471:12: error: 'QVariant::QVariant(void*)' is private
         inline QVariant(void *) Q_DECL_EQ_DELETE;
                ^
    ..\QtComuneQml\main.cpp:60:57: error: within this context
         context->setContextProperty("fileList", &contentList); // QVariant::fromValue(contentList));  // ?
    


  • Hope I finally solved ...
    The problem was the line

    property var fileList
    

    in the qml file that overwrites the one (same name) passed from the C++ code.



  • @SteMMo said in Not able to pass data from C++ to QML:

    Hope I finally solved ...

    If so, please don't forget to mark your post as such! Thanks.


Log in to reply