Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. My first attempt to create a calculator as a novice in Qt programming
Forum Updated to NodeBB v4.3 + New Features

My first attempt to create a calculator as a novice in Qt programming

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 6 Posters 8.9k Views 5 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #14

    Thank you very much.
    The first step is finished. I will think about the code well to understand every part properly. And think to start a new means to develop it. If I have question I will post them again.
    Bye for now.

    1 Reply Last reply
    0
    • tomyT Offline
      tomyT Offline
      tomy
      wrote on last edited by
      #15

      Hello guys, I'm back to this thread. :)

      I had a very little time for Qt (I'm involving many aspects of CS and scheduled my works :)) using which I went a few steps on the process of developing my fist app the way I mentioned in the first post of this thread.

      Anyway, Thanks for your patient and we will go to the game!

      The changes I make (just using the experience I have on C++) are as follows:

      My_First_Calculator.h

      #ifndef MY_FIRST_CALCULATOR_H
      #define MY_FIRST_CALCULATOR_H
      #include <QDialog>
      #include <QSignalMapper>
      #include <QStringList>
      
      class QLineEdit;
      
      class My_First_Calculator : public QDialog
      {
          Q_OBJECT
      public:
          My_First_Calculator(const QStringList& texts
                              ,QWidget* parent = 0);
      
      private slots:
          void show_number(const QString&);
          void reset();
      
      private:
          QLineEdit* lineEdit;
          QSignalMapper* signalMapper;
          QString temp_text;
      };
      
      #endif // MY_FIRST_CALCULATOR_H
      

      My_First_Calculator.cpp:

      #include <QtWidgets>
      #include "my_first_calculator.h"
      
      My_First_Calculator::My_First_Calculator(const QStringList& texts,
                QWidget* parent) : QDialog(parent)
      {
           lineEdit = new QLineEdit;
           QPushButton* quit = new QPushButton(tr("Close"));
           QPushButton* clear = new QPushButton(tr("C"));
      
           signalMapper = new QSignalMapper(this);
           QGridLayout* gridLayout = new QGridLayout;
      
           for(int i=0; i<texts.size(); ++i)
            {
               QPushButton* button = new QPushButton(texts[i]);
      
               connect(button, SIGNAL(clicked(bool)),
                       signalMapper, SLOT(map()));
      
               signalMapper -> setMapping(button, texts[i]);
               gridLayout -> addWidget(button, i/3, i%3);
            }
      
           connect(signalMapper, SIGNAL(mapped(QString)), this,
                   SLOT(show_number(QString)));
      
           connect(quit, SIGNAL(clicked(bool)), this, SLOT(close()));
           connect(clear, SIGNAL(clicked(bool)), this, SLOT(reset()));
      
           QVBoxLayout* layout = new QVBoxLayout;
           layout -> addWidget(lineEdit);
           layout -> addLayout(gridLayout);
           layout -> addWidget(quit);
           layout -> addWidget(clear);
           setLayout(layout);
      }
      
      //****************************
      
      void My_First_Calculator::show_number(const QString& text)
      {
          if(text == "C")
              temp_text.clear();
          else  temp_text.append(text);
          lineEdit -> setText(temp_text);
      }
      
      //*****************************************
      
      void My_First_Calculator::reset()
      {
          emit show_number("C");
      }
      

      main.cpp:

      #include <QApplication>
      #include "my_first_calculator.h"
      
      int main(int argc , char* argv[])
      {
          QApplication app(argc, argv);
      
          QStringList texts;
          texts << "0" << "1" << "2" << "3" << "4"
                << "5" << "6" << "7" << "8" << "9";
      
          My_First_Calculator myCal(texts);
          myCal.show();
      
          return app.exec();
      }
      

      At any given time, I try to add some more features to the app. but now two questions:)

      1- Is there any weak part in the code that should be replaced?
      2- Apparently since I didn't use the Designer for it, I can't change the form my mouse! Is it true? If so, what nifty means is there to change the shape of the form? I mean buttons, sizes, or whatsoever?

      ThankX!

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #16

        Hi
        All is debatable but my points would be :
        You are using good variable names but Im not crazy with underscores.
        Seen code where used a lot and it very hard to read.
        Not issue here but now its mentioned.
        Also , choose a style for variable naming and use it all over.
        QLineEdit* lineEdit; <<< qt style
        QSignalMapper* signalMapper; << qt style
        QString temp_text; << other style

        Also
        void My_First_Calculator::reset()
        why send a "c" to show_number to reset?
        Why not directly

        void My_First_Calculator::reset()
        {
        temp_text.clear();
        lineEdit -> setText("");
        }

        • 2- Apparently since I didn't use the Designer
          Well if you do not use UI files, then you just adjust from code.
          with resize or setGeometry.
          However, for app like yours, one would use layout to make it auto scale the buttons to any size if dialog is resized.
          http://doc.qt.io/qt-5/layout.html
        tomyT 1 Reply Last reply
        2
        • mrjjM mrjj

          Hi
          All is debatable but my points would be :
          You are using good variable names but Im not crazy with underscores.
          Seen code where used a lot and it very hard to read.
          Not issue here but now its mentioned.
          Also , choose a style for variable naming and use it all over.
          QLineEdit* lineEdit; <<< qt style
          QSignalMapper* signalMapper; << qt style
          QString temp_text; << other style

          Also
          void My_First_Calculator::reset()
          why send a "c" to show_number to reset?
          Why not directly

          void My_First_Calculator::reset()
          {
          temp_text.clear();
          lineEdit -> setText("");
          }

          • 2- Apparently since I didn't use the Designer
            Well if you do not use UI files, then you just adjust from code.
            with resize or setGeometry.
            However, for app like yours, one would use layout to make it auto scale the buttons to any size if dialog is resized.
            http://doc.qt.io/qt-5/layout.html
          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #17

          @mrjj said in My first attempt to create a calculator as a novice in Qt programming:

          Hi
          All is debatable but my points would be :
          You are using good variable names but Im not crazy with underscores.
          Seen code where used a lot and it very hard to read.

          Hi. :)

          You are right. It's hard for reading. I will put enough white spaces and comments when it is finished. :)
          And what do you mean by "not crazy with underscore" please? (not a native speaker. :( )

          Not issue here but now its mentioned.
          Also , choose a style for variable naming and use it all over.
          QLineEdit* lineEdit; <<< qt style
          QSignalMapper* signalMapper; << qt style
          QString temp_text; << other style

          How to choose a style please?

          Also
          void My_First_Calculator::reset()
          why send a "c" to show_number to reset?
          Why not directly

          void My_First_Calculator::reset()
          {
          temp_text.clear();
          lineEdit -> setText("");
          }

          Yeah, very handy, thanks. I applied it. :)

          • 2- Apparently since I didn't use the Designer
            Well if you do not use UI files, then you just adjust from code.
            with resize or setGeometry.
            However, for app like yours, one would use layout to make it auto scale the buttons to any size if dialog is resized.
            http://doc.qt.io/qt-5/layout.html

          Apparently, in my form until here, using only HBoxLayout, VBoxLayout, and GridLayout are fine. Do you not agree?

          mrjjM 1 Reply Last reply
          0
          • tomyT tomy

            @mrjj said in My first attempt to create a calculator as a novice in Qt programming:

            Hi
            All is debatable but my points would be :
            You are using good variable names but Im not crazy with underscores.
            Seen code where used a lot and it very hard to read.

            Hi. :)

            You are right. It's hard for reading. I will put enough white spaces and comments when it is finished. :)
            And what do you mean by "not crazy with underscore" please? (not a native speaker. :( )

            Not issue here but now its mentioned.
            Also , choose a style for variable naming and use it all over.
            QLineEdit* lineEdit; <<< qt style
            QSignalMapper* signalMapper; << qt style
            QString temp_text; << other style

            How to choose a style please?

            Also
            void My_First_Calculator::reset()
            why send a "c" to show_number to reset?
            Why not directly

            void My_First_Calculator::reset()
            {
            temp_text.clear();
            lineEdit -> setText("");
            }

            Yeah, very handy, thanks. I applied it. :)

            • 2- Apparently since I didn't use the Designer
              Well if you do not use UI files, then you just adjust from code.
              with resize or setGeometry.
              However, for app like yours, one would use layout to make it auto scale the buttons to any size if dialog is resized.
              http://doc.qt.io/qt-5/layout.html

            Apparently, in my form until here, using only HBoxLayout, VBoxLayout, and GridLayout are fine. Do you not agree?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #18

            Hi :)

            And what do you mean by "not crazy with underscore" please? (not a native speaker. :( )

            Means, I don't like so much. ( reading it )

            How to choose a style please?

            Well, Thats is mostly what you like. Sometimes in teams , we must agree but if you are
            working solo then just choose one and stick to it.
            Its mostly about if you write it like
            SomeName
            someName
            somename
            some_name

            so the important part is just to name it the same way each time and not mixing it.

            • Apparently, in my form until here, using only HBoxLayout, VBoxLayout, and GridLayout are fine. Do you not agree?
              Those are fine and should make it scale all when you resize the window.
            tomyT 1 Reply Last reply
            1
            • mrjjM mrjj

              Hi :)

              And what do you mean by "not crazy with underscore" please? (not a native speaker. :( )

              Means, I don't like so much. ( reading it )

              How to choose a style please?

              Well, Thats is mostly what you like. Sometimes in teams , we must agree but if you are
              working solo then just choose one and stick to it.
              Its mostly about if you write it like
              SomeName
              someName
              somename
              some_name

              so the important part is just to name it the same way each time and not mixing it.

              • Apparently, in my form until here, using only HBoxLayout, VBoxLayout, and GridLayout are fine. Do you not agree?
                Those are fine and should make it scale all when you resize the window.
              tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by tomy
              #19

              @mrjj said in My first attempt to create a calculator as a novice in Qt programming:

              Hi :)

              And what do you mean by "not crazy with underscore" please? (not a native speaker. :( )

              Means, I don't like so much. ( reading it )

              How to choose a style please?

              Well, Thats is mostly what you like. Sometimes in teams , we must agree but if you are
              working solo then just choose one and stick to it.
              Its mostly about if you write it like
              SomeName
              someName
              somename
              some_name

              so the important part is just to name it the same way each time and not mixing it.

              • Apparently, in my form until here, using only HBoxLayout, VBoxLayout, and GridLayout are fine. Do you not agree?
                Those are fine and should make it scale all when you resize the window.

              Thank you :)

              I put some other widgets as well, like another lineEdit for the results and so on.
              Now I come across two other questions. :)

              1- Assume I have a button like this:

              QPushButton* two = new QPushButton(tr("2"))
              

              How do I send that string ("2") to the show_number(const QString& text) so that it will be shown by the lineEdit in the form when running the program? I mean without using a signalMapper? Apparently I can't use parameters in connect to send for that function. :(

              2- Consider I want to store the contents the lineEdit into a vector of char (so that I will be able to calculate the contents later). How to do it lease?

              EDITED!

              jsulmJ 1 Reply Last reply
              0
              • tomyT tomy

                @mrjj said in My first attempt to create a calculator as a novice in Qt programming:

                Hi :)

                And what do you mean by "not crazy with underscore" please? (not a native speaker. :( )

                Means, I don't like so much. ( reading it )

                How to choose a style please?

                Well, Thats is mostly what you like. Sometimes in teams , we must agree but if you are
                working solo then just choose one and stick to it.
                Its mostly about if you write it like
                SomeName
                someName
                somename
                some_name

                so the important part is just to name it the same way each time and not mixing it.

                • Apparently, in my form until here, using only HBoxLayout, VBoxLayout, and GridLayout are fine. Do you not agree?
                  Those are fine and should make it scale all when you resize the window.

                Thank you :)

                I put some other widgets as well, like another lineEdit for the results and so on.
                Now I come across two other questions. :)

                1- Assume I have a button like this:

                QPushButton* two = new QPushButton(tr("2"))
                

                How do I send that string ("2") to the show_number(const QString& text) so that it will be shown by the lineEdit in the form when running the program? I mean without using a signalMapper? Apparently I can't use parameters in connect to send for that function. :(

                2- Consider I want to store the contents the lineEdit into a vector of char (so that I will be able to calculate the contents later). How to do it lease?

                EDITED!

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #20

                @tomy

                1. In the slot:
                QString s = reinterpret_cast<QPushButton*>(sender())->text();
                
                1. Why? If user enters number 123, why do you want to store this number in a vector like ['1', '2', '3']? It is actually a vector of strings. For that you can split the string, see http://doc.qt.io/qt-5/qstring.html#split

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                tomyT 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @tomy

                  1. In the slot:
                  QString s = reinterpret_cast<QPushButton*>(sender())->text();
                  
                  1. Why? If user enters number 123, why do you want to store this number in a vector like ['1', '2', '3']? It is actually a vector of strings. For that you can split the string, see http://doc.qt.io/qt-5/qstring.html#split
                  tomyT Offline
                  tomyT Offline
                  tomy
                  wrote on last edited by tomy
                  #21

                  @jsulm said in My first attempt to create a calculator as a novice in Qt programming:

                  @tomy

                  1. In the slot:
                  QString s = reinterpret_cast<QPushButton*>(sender())->text();
                  
                  1. Why? If user enters number 123, why do you want to store this number in a vector like ['1', '2', '3']? It is actually a vector of strings. For that you can split the string, see http://doc.qt.io/qt-5/qstring.html#split

                  Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.

                  Guys, I think the program is much more complicated than these items. The app should be able to separate the operands from operators. And also it should be able to observe the priority of the operators, for example:
                  2+3*4 to be 14 not 20.

                  What methods would you generally use for each step?

                  I'm thinking of two tools: first the Dijkstra's algorithm to make the statement a Reverse Polish Notation and then use a stack for calculating. Do you agree?

                  jsulmJ 1 Reply Last reply
                  0
                  • tomyT tomy

                    @jsulm said in My first attempt to create a calculator as a novice in Qt programming:

                    @tomy

                    1. In the slot:
                    QString s = reinterpret_cast<QPushButton*>(sender())->text();
                    
                    1. Why? If user enters number 123, why do you want to store this number in a vector like ['1', '2', '3']? It is actually a vector of strings. For that you can split the string, see http://doc.qt.io/qt-5/qstring.html#split

                    Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.

                    Guys, I think the program is much more complicated than these items. The app should be able to separate the operands from operators. And also it should be able to observe the priority of the operators, for example:
                    2+3*4 to be 14 not 20.

                    What methods would you generally use for each step?

                    I'm thinking of two tools: first the Dijkstra's algorithm to make the statement a Reverse Polish Notation and then use a stack for calculating. Do you agree?

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #22

                    @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                    Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.

                    But it can be stored as a vector of strings: ["123", "+", "256"] - that's what I said.
                    Vector of char would be: ['1', '2', '3', '+', '2', '5, '6']

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    tomyT 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                      Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.

                      But it can be stored as a vector of strings: ["123", "+", "256"] - that's what I said.
                      Vector of char would be: ['1', '2', '3', '+', '2', '5, '6']

                      tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by tomy
                      #23

                      @jsulm said in My first attempt to create a calculator as a novice in Qt programming:

                      @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                      Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.

                      But it can be stored as a vector of strings: ["123", "+", "256"] - that's what I said.
                      Vector of char would be: ['1', '2', '3', '+', '2', '5, '6']

                      Buttons send each number as a string; "1", "3", "+" and so on. When the user types "123+256" a string containing "123+256" will be shown on lineEdit. Hence, I should somehow parse to make operators and operands detached. That was why (at the time) I thought of a vector of chars.
                      But anyway, I'm thinking of a good algorithm to cover expressions like this, that is when we input all of this line once, it makes a correct result:
                      2+30-12/3+(4^8-1)+sqrt(65)*2.12

                      kshegunovK 1 Reply Last reply
                      0
                      • tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by
                        #24

                        Hi all,

                        It's done. :) :) :)
                        The app is finished and it works fine. Please download it from here:
                        http://www.4shared.com/file/H5brsoaPce/My_First_Calculator.html

                        It needs only a few .dll files for to be run.

                        I have some question please:
                        1- How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?
                        2- I want to design the buttons better from the size, position end etc. How can I do this?
                        3- Is it possible to make it appear like a professional app?

                        1 Reply Last reply
                        0
                        • tomyT tomy

                          @jsulm said in My first attempt to create a calculator as a novice in Qt programming:

                          @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                          Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.

                          But it can be stored as a vector of strings: ["123", "+", "256"] - that's what I said.
                          Vector of char would be: ['1', '2', '3', '+', '2', '5, '6']

                          Buttons send each number as a string; "1", "3", "+" and so on. When the user types "123+256" a string containing "123+256" will be shown on lineEdit. Hence, I should somehow parse to make operators and operands detached. That was why (at the time) I thought of a vector of chars.
                          But anyway, I'm thinking of a good algorithm to cover expressions like this, that is when we input all of this line once, it makes a correct result:
                          2+30-12/3+(4^8-1)+sqrt(65)*2.12

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #25

                          @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                          But anyway, I'm thinking of a good algorithm to cover expressions like this, that is when we input all of this line once, it makes a correct result:

                          Reverse polish notation is what you're probably searching for. See here.

                          Read and abide by the Qt Code of Conduct

                          VRoninV 1 Reply Last reply
                          0
                          • kshegunovK kshegunov

                            @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                            But anyway, I'm thinking of a good algorithm to cover expressions like this, that is when we input all of this line once, it makes a correct result:

                            Reverse polish notation is what you're probably searching for. See here.

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by VRonin
                            #26

                            This is a infix to postfix conversion for the 4 basic operations and parenthesis I wrote some time ago. it works just for integers but it's a start. once you have the string in postfix (i.e. reverse polish) the calculation is very easy

                            Edit

                            Ignore my sh*ty code, see http://rosettacode.org/wiki/Parsing/Shunting-yard_algorithm#C for an idea of converting infix to postfix. thanks @kshegunov for talking some sense into me

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            0
                            • tomyT Offline
                              tomyT Offline
                              tomy
                              wrote on last edited by tomy
                              #27

                              Thank you guys but the work is done!
                              I appreciate your reactions so much.
                              Please help me on these questions:

                              @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                              Hi all,

                              It's done. :) :) :)
                              The app is finished and it works fine. Please download it from here:
                              http://www.4shared.com/file/H5brsoaPce/My_First_Calculator.html

                              It needs only a few .dll files for to be run.

                              I have some question please:
                              1- How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?
                              2- I want to design the buttons better from the size, position end etc. How can I do this?
                              3- Is it possible to make it appear like a professional app?

                              kshegunovK 1 Reply Last reply
                              0
                              • tomyT tomy

                                Thank you guys but the work is done!
                                I appreciate your reactions so much.
                                Please help me on these questions:

                                @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                                Hi all,

                                It's done. :) :) :)
                                The app is finished and it works fine. Please download it from here:
                                http://www.4shared.com/file/H5brsoaPce/My_First_Calculator.html

                                It needs only a few .dll files for to be run.

                                I have some question please:
                                1- How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?
                                2- I want to design the buttons better from the size, position end etc. How can I do this?
                                3- Is it possible to make it appear like a professional app?

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #28

                                @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                                How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?

                                You can use QWidget::setText on the main window. That should work.

                                I want to design the buttons better from the size, position end etc. How can I do this?

                                I haven't downloaded the code as I haven't the time to check it out just now, but putting the widgets in layouts should arrange them well.

                                Is it possible to make it appear like a professional app?

                                Well, you could polish the look a bit with a stylesheet, look up the docs on them.

                                Read and abide by the Qt Code of Conduct

                                tomyT 1 Reply Last reply
                                0
                                • kshegunovK kshegunov

                                  @tomy said in My first attempt to create a calculator as a novice in Qt programming:

                                  How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?

                                  You can use QWidget::setText on the main window. That should work.

                                  I want to design the buttons better from the size, position end etc. How can I do this?

                                  I haven't downloaded the code as I haven't the time to check it out just now, but putting the widgets in layouts should arrange them well.

                                  Is it possible to make it appear like a professional app?

                                  Well, you could polish the look a bit with a stylesheet, look up the docs on them.

                                  tomyT Offline
                                  tomyT Offline
                                  tomy
                                  wrote on last edited by
                                  #29

                                  @kshegunov
                                  Thank you. Please download the app and look how it appears. Until that time I will read the link on docs. Thanks.

                                  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