Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Not able to pass data from C++ to QML

    General and Desktop
    3
    7
    308
    Loading More Posts
    • 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.
    • SteMMo
      SteMMo last edited by SteMMo

      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 Reply Quote 0
      • SteMMo
        SteMMo last edited by

        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. Rogina 1 Reply Last reply Reply Quote 1
        • X
          Xyrer last edited by

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

          SteMMo 1 Reply Last reply Reply Quote 0
          • SteMMo
            SteMMo last edited by

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

            1 Reply Last reply Reply Quote 0
            • SteMMo
              SteMMo last edited by SteMMo

              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 Reply Quote 0
              • SteMMo
                SteMMo @Xyrer last edited by

                @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 Reply Quote 0
                • SteMMo
                  SteMMo last edited by

                  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. Rogina 1 Reply Last reply Reply Quote 1
                  • Pablo J. Rogina
                    Pablo J. Rogina @SteMMo last edited by

                    @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 Reply Quote 0
                    • First post
                      Last post