Results 1 to 6 of 6

Thread: How to call cpp file function from qml??

  1. #1
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Post How to call cpp file function from qml??

    Hi All,
    As i am new to qt quick i'm in need of calling a function from qml to cpp file, I am not able to write code for this. I tried this code...


    I am trying to add two numbers!!!I tried writing it in qml but could not get the result.. so i am looking to write a function for adding the nos. in the cpp file .
    i would call from qml to cpp function...

    QML CODE:
    javascript Code:
    1. property string i;
    2. property string j;
    3.  
    4. Button {
    5. id: one
    6. x: 59
    7. y: 131
    8. width: 41
    9. height: 42
    10. text: "1"
    11. onClicked:
    12. {
    13.  
    14. i = text
    15. textfield.text = textfield.text + i
    16. }
    17. }
    18. Button {
    19. id: two
    20. x: 119
    21. y: 131
    22. text: "2"
    23. onClicked:
    24. {
    25.  
    26. i = text
    27. textfield.text = textfield.text + i
    28.  
    29. }
    30.  
    31. }
    32.  
    33. Button {
    34. id: add
    35. x: 247
    36. y: 131
    37. text: "+"
    38. onClicked:
    39. {
    40. j = textfield.text
    41. ans = j
    42.  
    43. textfield.text = ""
    44.  
    45. funct.buttonclicked()
    To copy to clipboard, switch view to plain text mode 

    CPP CODE:
    Qt Code:
    1. void funct::buttonclicked()
    2. {
    3.  
    4. QString *s = new QString;
    5. s->show();
    6. .....???
    7. .....???
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    I am not able write from this!!!!

    Thanks in advance and Regards,
    Naufal.A
    Last edited by wysota; 24th January 2012 at 16:32. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to call cpp file function from qml??

    Export your C++ object to QML using QDeclarativeContext::setContextProperty().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Post Re: How to call cpp file function from qml??

    Hi,
    Can i have some more explanation on it sir.
    I could not understand it clearly!!!!

    Thanks and Regards,
    Naufal.A

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to call cpp file function from qml??

    What's wrong with the examples from Qt docs on using context properties in QML?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to call cpp file function from qml??

    Hi,
    I am getting that cleared now.. i have one more doubt in this!!!
    How to send the integer value from cpp fn to qml fn??
    I have tried using QDeclarative view and also Qdeclarative context and i am getting only errors..

    Error: cannot convert 'QGraphicsObject*' to 'QObject*' in initialization


    QDeclarativeView view;
    QObject *object = view.rootObject();
    QObject* listView = object->findChild<QObject *>("events");
    QVariant returnedValue;
    QMetaObject::invokeMethod(listView, "addd",Q_RETURN_ARG(QVariant, returnedValue));

    Can some one help me to find whats the mistake is

    Thanks in advance and Regards,
    Naufal.A

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to call cpp file function from qml??

    It's likely you are missing #include <QGraphicsObject>
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt function call in vb.net
    By abghosh in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2010, 18:00
  2. Call to database function.
    By retto in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2009, 13:29
  3. call function as Qstring
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 02:35
  4. function call
    By Walsi in forum Qt Programming
    Replies: 3
    Last Post: 12th June 2007, 10:13
  5. Cannot call function without object
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 11th June 2007, 15:55

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.