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. How to perform addition in qml by calling .cpp function?
Forum Updated to NodeBB v4.3 + New Features

How to perform addition in qml by calling .cpp function?

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 2.9k 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.
  • M Offline
    M Offline
    mkhpad
    wrote on last edited by
    #1

    I am having a .qml file which contains four buttons as: first button with text: "3", second button with "5", third with the operator symbol "+" and the fourth button with "=" .... Now what i need is when i clicked on first/second button it must store the value int a variable and when clicked on "+",i had created a function that should be calling from qml to cpp file,where it should perform the addition operation. Then,the value must be passed again to the qml and display it in a textarea.when pressed the = button.

    Here is how i have tried on this:

    .qml file

    @
    property string i;
    property string c;

    Button {
    id: button1
    x: 203
    y: 258
    text: "5"

            onClicked: {
    
                    i = text
                    t.text = t.text + i
    
            }
        }
    

    Button {
    id: button2
    x: 203
    y: 258
    text: "3"

            onClicked: {
    
                    i = text
                    t.text = t.text + i
    
            }
        }
    

    Button {
    id: buttonplus
    x: 136
    y: 320
    text: "+"

    onClicked: {
    StringHelper.apple()

    }
    }

    Button {
    id: equal
    x: 195
    y: 320
    width: 116
    height: 42
    text: "="

            onClicked: {
    
    
              t.text = c
    
            }
        }
    

    TextField {
    id: t
    x: 81
    y: 60
    width: 225
    height: 50
    text: ""
    maximumLength: 32765

    }
    @

    stringhelper.h

    @
    #ifndef STRINGHELPER_H
    #define STRINGHELPER_H
    #include<QObject>

    class StringHelper : public QObject
    {
    Q_OBJECT
    public slots:
    Q_INVOKABLE int apple(int x,int y)

        {
    

    int c = x + y;

    ????????????????????????
    }
    @

    main.cpp:

    @
    #include <QApplication>
    #include <QDeclarativeView>
    #include <QDeclarativeContext>
    #include "stringhelper.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    StringHelper stringHelper;
    
    QDeclarativeView view;
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.rootContext()->setContextProperty("StringHelper", &stringHelper);
    view.setSource(QUrl("qml/main.qml"));
    

    #if defined(Q_WS_S60) || defined(Q_WS_MAEMO)
    view.showMaximized();
    #else
    view.setGeometry(100, 100, 800, 480);
    view.show();
    #endif

    return a.exec;
    

    }
    @

    So, Kindly help in finding out!!!

    Thanks and Regards

    [EDIT: code formatting, please wrap in @-tags, Volker]

    Harish.M

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Just return the value in <code>int apple(int x, int y) { ... return c;}</code> and pass it to the desired variable or item in QML <code>onClicked: { t.text = StringHelper.apple(button1.text, button2.text) }</code>.

      Is there any particular reason you are performing the addition in C++? QML event handlers take any kind of JavaScript code, including mathematical expressions.

      As to the your post: Your code formatting is seriously broken. Wrap code in @ tags so its gets formatted properly. Even your non-code part is seriously broken due to the hypens. Either use the preview function or the edit function to make sure your post is actually readable next time.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mkhpad
        wrote on last edited by
        #3

        Thank you for your reply Sir....

        I tried what you have said sir but it shows -> Error: Insufficient arguments

        I have changed my coding as:

        onClicked: {

                    t.text = StringHelper.apple();
        

        and also as:

        onClicked: {

                    t.text = StringHelper.apple(c);
        
                }
        
                }
        

        I am trying to add calculator in my app sir thats why i am in need of it and i am not using Javascript code.

        any comments.....

        Harish.M

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          You will have, of course, pass <code>button1.text</code> and <code>button2.text</code> to <code>StringHelper::apple(int a, int b)</code>.
          @
          onClicked: {
          t.text = StringHelper.apple(button1.text, button2.text);
          }
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mkhpad
            wrote on last edited by
            #5

            Thank You for your answer....

            You are really great sir i was trying this for the past 2 days and you are the one who had come out with the solution...

            Thank you again Sir....:)
            :)

            Sir suppose if i am having 0-9 buttons and in this case if i need to perform addition operation how could i declare the result function....

            onClicked: {

                        t.text = StringHelper.apple(????????????????);
            
                    }
            

            Thanks,
            Harish

            Harish.M

            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