Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Not able to pass data from C++ to QML
QtWS25 Last Chance

Not able to pass data from C++ to QML

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 669 Views
  • 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.
  • SteMMoS Offline
    SteMMoS Offline
    SteMMo
    wrote on last edited by SteMMo
    #1

    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

    1 Reply Last reply
    0
    • SteMMoS Offline
      SteMMoS Offline
      SteMMo
      wrote on last edited by
      #6

      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.

      Pablo J. RoginaP 1 Reply Last reply
      1
      • X Offline
        X Offline
        Xyrer
        wrote on last edited by
        #2

        @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?

        SteMMoS 1 Reply Last reply
        0
        • SteMMoS Offline
          SteMMoS Offline
          SteMMo
          wrote on last edited by
          #3

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

          1 Reply Last reply
          0
          • SteMMoS Offline
            SteMMoS Offline
            SteMMo
            wrote on last edited by SteMMo
            #4

            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
            
            
            1 Reply Last reply
            0
            • X Xyrer

              @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?

              SteMMoS Offline
              SteMMoS Offline
              SteMMo
              wrote on last edited by
              #5

              @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));  // ?
              
              1 Reply Last reply
              0
              • SteMMoS Offline
                SteMMoS Offline
                SteMMo
                wrote on last edited by
                #6

                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.

                Pablo J. RoginaP 1 Reply Last reply
                1
                • SteMMoS SteMMo

                  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.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #7

                  @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.

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  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