Results 1 to 6 of 6

Thread: Exposing qVariant qml c++ vice versa

  1. #1
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Exposing qVariant qml c++ vice versa

    Hello friends,

    is it possible to expose qvariant datatype to qml or vice versa?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Exposing qVariant qml c++ vice versa

    Yes.

    If you have a fixed value, you can set it as a context property.
    If it is a changing value, you will need a QObject subclass and a Q_PROPERTY with type QVariant and then set an instance of that class as a context property.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Exposing qVariant qml c++ vice versa

    Isn't it a little bit overhead. I just want return a value of a method which is a member of a cpp class.

    Imagine following sample:
    var result = cppobject.method ();

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Exposing qVariant qml c++ vice versa

    You can do that as well.

    Qt Code:
    1. class SomeClass : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Q_INVOKABLE QVariant method();
    7. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Exposing qVariant qml c++ vice versa

    Thank you for your suggestions, but it does not compile.
    it says: Error: C2079: '_r' uses undefined class 'QVariant'
    Error: C2027: use of undefined type 'QVariant'

    I add QT += core and #include the relevant <QVariant> but nothing helps. How do I schould implement the function?

    I do a simple stuff to test the exposing through qml:

    QVariant SomeClass::method(){
    QVariant var(123);
    return var;
    }

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Exposing qVariant qml c++ vice versa

    QT += core is not necessary, that is implied by any other Qt module.

    Undefined type usually means that the include is missing.
    Maybe you added the include to the wrong file?

    For which file do you get the error?

    Or maybe forgot the forward declaration in the header of SomeClass?
    Can you post header and source for SomeClass?

    Cheers,
    _

Similar Threads

  1. kde pid from window id and vice versa
    By sky in forum KDE Forum
    Replies: 2
    Last Post: 18th May 2013, 09:17
  2. Qstring to Qdate and vice versa
    By sai_3289 in forum Qt Programming
    Replies: 10
    Last Post: 12th February 2013, 11:54
  3. Replies: 3
    Last Post: 13th May 2011, 13:46
  4. conversion between string to hex and vice versa
    By mohanakrishnan in forum Newbie
    Replies: 2
    Last Post: 5th December 2009, 11:25
  5. Convert QByteArray to object and vice versa
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 20:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.