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. SetContextProperty of a class that extends Object
Forum Updated to NodeBB v4.3 + New Features

SetContextProperty of a class that extends Object

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 6.6k 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.
  • J Offline
    J Offline
    javiyt
    wrote on last edited by
    #1

    Hi,

    I saw in this website how to make a class that extends Object to set an instance as a property.

    http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#embedding-c-objects-into-qml-components

    I did it as they say but I had some troubles and finally I had to separate to another file the class, but when I try to do this

    @ApplicationData data;
    view.rootContext()->setContextProperty("applicationData", &data);@

    it gives this error:

    'QVariant::QVariant(void*)' is private

    I have tried to search the problem, and read about the classes priviledges but I don't know how to give an object as a property then. Thank you in advance.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Peppy
      wrote on last edited by
      #2

      I think you have to write much more of code...
      Maybe you forgot Q_OBJECT macro...

      1 Reply Last reply
      0
      • J Offline
        J Offline
        javiyt
        wrote on last edited by
        #3

        Thank you for the fast answer.

        I did just what the example was saying

        @class ApplicationData : public QObject
        {
        Q_OBJECT
        public:
        Q_INVOKABLE QDateTime getCurrentDateTime() const {
        return QDateTime::currentDateTime();
        }
        };
        @

        Well I had to write it in a separated file with the headers but it doesn't complaint about that, just gives back the error QVariant::QVariant(void*)'

        1 Reply Last reply
        0
        • J Offline
          J Offline
          javiyt
          wrote on last edited by
          #4

          I discovered the problem but not the solution. If I delete the macro Q_OBJECT from the code it doesn't give any problem, but I can not call the functions inside from the QML. But when I include it in the code it gives the problem:

          @ undefined reference to `vtable for ...' @

          1 Reply Last reply
          0
          • R Offline
            R Offline
            renato.filho
            wrote on last edited by
            #5

            you need keep the Q_OBJECT macro (used on moc process), this is necessary to make your class a QObject.

            try call the function with explicitly cast:
            view.rootContext()->setContextProperty("applicationData", (QObject*)&data);

            1 Reply Last reply
            0
            • J Offline
              J Offline
              javiyt
              wrote on last edited by
              #6

              Yes I included it, I wrote in the header Q_OBJECT as the example but it's all the time appearing this error.

              @undefined reference to `vtable for ...'@

              With the explicitly cast it doesn't give me the other error. It's a beginning, thanks!

              1 Reply Last reply
              0
              • R Offline
                R Offline
                renato.filho
                wrote on last edited by
                #7

                Are you using qmake?
                if yes, try put the Q_OBJECT macro, then re-run the qmake program, after that compile your program.

                A moc file need be genereted and included on your src list. (qmake do this for you)

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  javiyt
                  wrote on last edited by
                  #8

                  I was doing it wrong, because what you said, I was doing it copying it directly. I discovered the way to add it correctly with Qtcreator.

                  Really helpfull, Thanks a lot!

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lemma
                    wrote on last edited by
                    #9

                    Use QVariant::fromValue:
                    @view.rootContext()->setContextProperty("applicationData", QVariant::fromValue(data));@

                    http://qt-project.org/doc/qt-4.8/declarative-modelviews-stringlistmodel-main-cpp.html

                    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