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. Sum 2 integers
Forum Updated to NodeBB v4.3 + New Features

Sum 2 integers

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 7.7k 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.
  • V Offline
    V Offline
    Vesco
    wrote on last edited by
    #1

    Hi all, i need to do a presentation of Qt Designer for my class.
    So i'm going to do an application which does the sum between 2 integers and place the result into a TextBox.
    But i'm having trouble making it.
    I hope you guys can help me!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      Welcome to forum!

      Where exactly are you stopped?

      Just a guess, may be at the int to QString part ? try this
      @ui->lineEdit->setText(QString::number(1234));@


      Declaration of (Platform) independence.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vesco
        wrote on last edited by
        #3

        I don't understand how to make this application in qt designer..

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rahul Das
          wrote on last edited by
          #4

          okay... Have you tried anything at all ?

          You can start "here":http://www.developer.nokia.com/Community/Wiki/How_to_use_Qt_Creator_IDE and "there":https://qt-project.org/videos


          Declaration of (Platform) independence.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            Hi,

            Kindly provide some code/images that will be more easy for us to understand where exactly u need help. But dont expect us to do your homework :)

            Welcome to the forum.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              Vesco
              wrote on last edited by
              #6

              So far i did:
              created a new project like this Qt Widget Project -> Qt Gui Application
              Then in the Designer i placed 3 textEdit.
              I know the Signals Slots functions, but i can't make a sum between this 2 integers..

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rahul Das
                wrote on last edited by
                #7

                so far, so good.

                How do you do it in regular c++ ?? same way you do addition!!

                and i have already given you the code to set text.


                Declaration of (Platform) independence.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  soroush
                  wrote on last edited by
                  #8

                  Each line edit holds its text value in a QString object. You can access values by "QLineEdit::text":http://qt-project.org/doc/qt-4.8/qlineedit.html#text-prop . You need to convert text values of QLineEdit s to integer. It's done by "QString::toInt":http://qt-project.org/doc/qt-4.8/qstring.html#toInt . Then just add your numbers, and put result in desired QLineEdit. Convert integer to QString by "QString::number":http://qt-project.org/doc/qt-4.8/qstring.html#number-4 and set text of QLineEdit.

                  Hope that helps.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    Vesco
                    wrote on last edited by
                    #9

                    But i don't know how to make functions, i mean in c++ is a lot more easy..
                    like:
                    @
                    int Sum(int a, int b){
                    return a+b;
                    }
                    @
                    now i'm felling like i'm lost.. i thought Designer was a lot more easier..

                    By default Qt made this:
                    @#include "mainwindow.h"
                    #include "ui_mainwindow.h"

                    MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                    {
                    ui->setupUi(this);
                    }

                    MainWindow::~MainWindow()
                    {
                    delete ui;
                    }
                    @

                    [Edit: Please add @ tags around your code; mlong]

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      Vesco
                      wrote on last edited by
                      #10

                      KK guys i've made it thanks!
                      Instead of "->" i've used "."
                      Thanks =)

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sam
                        wrote on last edited by
                        #11

                        [quote author="Vesco" date="1337023716"]

                        now i'm felling like i'm lost.. i thought Designer was a lot more easier..

                        [/quote]

                        The Qt Designer makes things far more easier when we are creating a forms and adding widgets. The more you practice the more you learn. So as you have already added three textbox/lineEdit you can further set the objectName in the property editor as lineEditOne , lineEditTwo and lineEditThree. Also you can further add button/pushButton and set the objectName as btnAdd.

                        So that the user can enter the values in the lineEdit and Click on Add button to display the result in lineEditThree.

                        You can try the following code on_btnAdd_Clicked() slot(you can create through designer)
                        @
                        void MainWindow::on_btnAdd_clicked()
                        {
                        int a = ui->lineEditOne->text().toInt();
                        int b = ui->lineEditTwo->text().toInt();
                        int result = a+b;
                        ui->lineEditThree->setText(QString::number(result));

                        }@

                        Try this if it works. As a beginner you can start with some "Video tutorials ":http://www.voidrealms.com/tutorials.aspx?filter=qt

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          Vesco
                          wrote on last edited by
                          #12

                          Ok, it works, but I do have a question.
                          What's the differences between QString::number(); and .toInt(); ?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Sam
                            wrote on last edited by
                            #13

                            The lineEdit::text() returns a QString so in order to assign it to an integer value we need to cast it, so we use .toInt(). Similarly when we are setting the value to the lineEdit it has to be a QString. So we cast it from int to QString using QString::number();

                            Look at Soroush's post above.

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              Vesco
                              wrote on last edited by
                              #14

                              Ok, thanks you all!

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                Sam
                                wrote on last edited by
                                #15

                                Kindly Edit your first post and add [Solved] to the title.

                                Happy Coding :)

                                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