Results 1 to 11 of 11

Thread: How to perform calculator functions in qt qml?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to perform calculator functions in qt qml?

    So what have you tried?
    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.


  2. #2
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Post Re: How to perform calculator functions in qt qml?

    I had tried the operation by clicking "=" and not "+" but still i was not able to get the required output.

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

    Default Re: How to perform calculator functions in qt qml?

    How does the code look like? Did you implement the stack of operations?
    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.


  4. #4
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Unhappy Re: How to perform calculator functions in qt qml?

    If I change my coding as:

    a = t.text

    b = t.text

    c = a + b

    for eg: a = t.text = "2"
    b = t.text = "3"

    c = a + b.......i.e. c = 2 + 3

    and when clicked on "=" my answer should be "5" but i am getting the values appended as "2323"..


    Can someone please help me....?



    Whether the calculation done in .qml file is right or else i should i create a function call to .cpp file and do the calculation there....I had tried that too but still i was not able to get the result.....

    Is there anyone to solve my problem?

    Last edited by harish; 30th January 2012 at 05:59.

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

    Default Re: How to perform calculator functions in qt qml?

    You are working on text so the + operator concatenates. Your program doesn't know you want it to convert text to integers.

    Doing calculations in qml is fine. Have you seen the Calculator example?
    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.


  6. #6
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Post Re: How to perform calculator functions in qt qml?

    I had already found the link which have given sir.In this they are using javascript code but i am using C++.

    Now i had tried anotherway of doing my app as:

    I had made a function call to .cpp file and done the calcultion there and passed that value to the .qml file.

    Here is my code:

    .qml file

    javascript Code:
    1. property string i;
    2. property string c;
    3. Button {
    4. id: button1
    5. x: 203
    6. y: 258
    7. text: “5”
    8. onClicked:
    9. {
    10. i = text t.text = t.text + i
    11. }
    12. }
    13.  
    14. Button {
    15. id: button2
    16. x: 203
    17. y: 258
    18. text: “3”
    19. onClicked: { i = text t.text = t.text + i
    20. }
    21. }
    22.  
    23. Button {
    24. id: buttonplus
    25. x: 136
    26. y: 320
    27. text: “+” onClicked: {
    28. t.text = a;
    29. t.text = b;
    30.  
    31. StringHelper.apple(a,b);
    32. }
    33. }
    34.  
    35. Button {
    36. id: equal
    37. x: 195
    38. y: 320
    39. width: 116
    40. height: 42
    41. text: “=”
    42. onClicked: {
    43.  
    44. t.text = StringHelper.apple(button1.text,button2.text);
    45.  
    46. }
    47. }
    48.  
    49. TextField { id: t
    50. x: 81
    51. y: 60
    52. width: 225
    53. height: 50
    54. text: “” maximumLength: 32765
    55.  
    56. }
    To copy to clipboard, switch view to plain text mode 

    stringhelper.h


    Qt Code:
    1. #ifndef STRINGHELPER_H
    2. #define STRINGHELPER_H
    3.  
    4. #include<QObject>
    5.  
    6. class StringHelper : public QObject
    7. {
    8. Q_OBJECT
    9. public slots:
    10. int apple(int a,int b)
    11.  
    12. {
    13.  
    14. int c = a + b;
    15.  
    16. return c;
    17.  
    18. }
    19.  
    20.  
    21. };
    22.  
    23. #endif // STRINGHELPER_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp:

    Qt Code:
    1. #include <QApplication>
    2. #include <QDeclarativeView>
    3. #include <QDeclarativeContext>
    4. #include "stringhelper.h"
    5. #include<QDateTime>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. StringHelper stringHelper;
    12.  
    13. QDeclarativeView view;
    14. view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    15. view.rootContext()->setContextProperty("StringHelper", &stringHelper);
    16. view.setSource(QUrl("qml/sad/main.qml"));
    17.  
    18. #if defined(Q_WS_S60) || defined(Q_WS_MAEMO)
    19. view.showMaximized();
    20. #else
    21. view.setGeometry(100, 100, 800, 480);
    22. view.show();
    23. #endif
    24.  
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 



    From this i was able to get the addition operation performed and displayed but in case if i am having 10 buttons with integers from 0-9 how to declare the result function:


    in .qml file:

    javascript Code:
    1. onClicked: {
    2. t.text = StringHelper.apple(????????????????);
    3. }
    To copy to clipboard, switch view to plain text mode 

    ???????


    Could you please come out with a solution for my problem.....?
    Last edited by wysota; 30th January 2012 at 10:20. Reason: missing [code] tags

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

    Default Re: How to perform calculator functions in qt qml?

    Quote Originally Posted by harish View Post
    I had already found the link which have given sir.In this they are using javascript code but i am using C++.
    Last code you pasted was definitely using javascript.

    From this i was able to get the addition operation performed and displayed but in case if i am having 10 buttons with integers from 0-9 how to declare the result function:


    in .qml file:

    javascript Code:
    1. onClicked: {
    2. t.text = StringHelper.apple(????????????????);
    3. }
    To copy to clipboard, switch view to plain text mode 

    Could you please come out with a solution for my problem.....?
    javascript Code:
    1. t.text = (Number(a)+Number(b)).toString()
    To copy to clipboard, switch view to plain text mode 
    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.


  8. #8
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Smile Re: How to perform calculator functions in qt qml?

    Ya thank you sir i got it....

    Its been a good guidance given by you sir...


    Thanks a lot again...


Similar Threads

  1. how to perform search in QTableWidget?
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2012, 04:25
  2. Perform an automatic click on a webpage
    By manuelito2459 in forum Newbie
    Replies: 0
    Last Post: 25th December 2011, 21:50
  3. Replies: 5
    Last Post: 27th May 2011, 02:51
  4. Perform a cyclic task in a QState
    By schall_l in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2010, 23:18
  5. Replies: 12
    Last Post: 22nd March 2010, 08:59

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
  •  
Qt is a trademark of The Qt Company.