Sum Variable Problem



  • 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



  • 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.



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



  • @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!



  • @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



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



  • @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



  • 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


  • Qt Champions 2016

    @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 :)



  • @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



  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.