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 Variable Problem
Forum Updated to NodeBB v4.3 + New Features

Sum Variable Problem

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 2.4k Views 2 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.
  • S Offline
    S Offline
    Semara123
    wrote on 20 Dec 2016, 12:13 last edited by
    #1

    excuse me sorry i want to ask im a newbie want to learn Qt but i have some problem

    i want to make Sum Variable that it will sum the variable and print it to the lineedit

    here for mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
         ui->label->setNum(0);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        ui->label->setText("SempAK Naga Jantan :v");
    }
    
    
    
    void MainWindow::on_pushButton_2_clicked()
    {
        int a,b,c;
        a= 2;
        b= 2;
        c=a+b;
    
        //QString hasil = "";
        ui->label->setNum(QString::number(ui->lineEdit->text()));
        //ui->label->text().toInt();
    }
    
    

    i already watch the tutorial on youtube but i still dont understand , whats wrong in my code T_T

    Thanks for help

    Sorry for my bad english

    1 Reply Last reply
    0
    • M Offline
      M Offline
      m.sue
      wrote on 20 Dec 2016, 12:42 last edited by m.sue
      #2

      Hi,
      setNum expects to get a number, you give it QString::number(ui->lineEdit->text()) which is a QString. And QString::number itself expects a number, you give it ui->lineEdit->text()which is a QString.
      -Michael.

      1 Reply Last reply
      2
      • B Offline
        B Offline
        BjornW
        wrote on 20 Dec 2016, 12:45 last edited by
        #3

        What does the compiler say? It's usually a good place to start :-)

        1 Reply Last reply
        2
        • B Offline
          B Offline
          Buckwheat
          wrote on 20 Dec 2016, 12:56 last edited by
          #4

          @Semara123 ... Welcome!

          1. I don't see you using "c" anywhere.
          2. Try using ui->label->setNum (ui->lineEdit->text ().toInt ());

          For more information on QString goto:
          http://doc.qt.io/qt-5/qstring.html

          Don't get discouraged. Qt is a large API and we all ask questions (even those of us who have been doing this for decades!) about it when we have problems. Good luck and have fun learning!

          Dave Fileccia

          S 1 Reply Last reply 20 Dec 2016, 13:26
          2
          • B Buckwheat
            20 Dec 2016, 12:56

            @Semara123 ... Welcome!

            1. I don't see you using "c" anywhere.
            2. Try using ui->label->setNum (ui->lineEdit->text ().toInt ());

            For more information on QString goto:
            http://doc.qt.io/qt-5/qstring.html

            Don't get discouraged. Qt is a large API and we all ask questions (even those of us who have been doing this for decades!) about it when we have problems. Good luck and have fun learning!

            S Offline
            S Offline
            Semara123
            wrote on 20 Dec 2016, 13:26 last edited by
            #5

            @Buckwheat said in Sum Variable Problem:

            ui->label->setNum (ui->lineEdit->text ().toInt ());

            i already did it as you said but nothing happened :(

            here is the code

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                 ui->label->setNum(0);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::on_pushButton_clicked()
            {
                ui->label->setText("SempAK Naga Jantan :v");
            }
            
            
            
            void MainWindow::on_pushButton_2_clicked()
            {
                int a,b,c;
                a= 2;
                b= 2;
                c=a+b;
            
                //QString hasil = "";
                ui->label->setNum (ui->lineEdit->text().toInt());
                //ui->label->text().toInt();
            }
            
            

            Sorry to bother you T_T

            and sorry for my bad english

            1 Reply Last reply
            0
            • M Offline
              M Offline
              m.sue
              wrote on 20 Dec 2016, 13:29 last edited by
              #6

              Hi,
              so what would you want in ui->label? The value of c, then just write ui->label->setNum(c).
              -Michael.

              1 Reply Last reply
              1
              • B Offline
                B Offline
                Buckwheat
                wrote on 20 Dec 2016, 14:42 last edited by
                #7

                @m-sue ... If that is the intent! Otherwise you are just adding a number for the sake of adding a number! And to top it off, it is a local variable so the compiler will most likely throw it away during optimization

                Dave Fileccia

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Semara123
                  wrote on 20 Dec 2016, 15:20 last edited by Semara123
                  #8

                  well now i already found the solution i forgot to delete this

                  case 2: _t->on_label_windowIconTextChanged((*reinterpret_cast< const QString(*)>(_a[1]))); break;
                  
                  in moc_mainwindow.cpp 
                  

                  now everything runs normally

                  thank you everybody for helping me to fix this problem :D

                  mrjjM 1 Reply Last reply 20 Dec 2016, 15:40
                  0
                  • S Semara123
                    20 Dec 2016, 15:20

                    well now i already found the solution i forgot to delete this

                    case 2: _t->on_label_windowIconTextChanged((*reinterpret_cast< const QString(*)>(_a[1]))); break;
                    
                    in moc_mainwindow.cpp 
                    

                    now everything runs normally

                    thank you everybody for helping me to fix this problem :D

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 20 Dec 2016, 15:40 last edited by
                    #9

                    @Semara123 said in Sum Variable Problem:

                    moc_mainwindow.cpp

                    This is a generated file so running qmake will recreate it.
                    So hopefully it wont come back :)

                    1 Reply Last reply
                    3
                    • B Offline
                      B Offline
                      Buckwheat
                      wrote on 21 Dec 2016, 13:10 last edited by
                      #10

                      @Semara123 ... you are manually editing your moc_ file? Wow! Ballsy! I just let the qmake regen it for me after I change my code. :D

                      Dave Fileccia

                      S 1 Reply Last reply 21 Dec 2016, 14:23
                      0
                      • B Buckwheat
                        21 Dec 2016, 13:10

                        @Semara123 ... you are manually editing your moc_ file? Wow! Ballsy! I just let the qmake regen it for me after I change my code. :D

                        S Offline
                        S Offline
                        Semara123
                        wrote on 21 Dec 2016, 14:23 last edited by
                        #11

                        @Buckwheat said in Sum Variable Problem:

                        @Semara123 ... you are manually editing your moc_ file? Wow! Ballsy! I just let the qmake regen it for me after I change my code. :D

                        LOL i think i didnt do anything :v, but i want to say thanks to you for helping me :D and thanks for other people :D

                        1 Reply Last reply
                        0

                        1/11

                        20 Dec 2016, 12:13

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved