Results 1 to 4 of 4

Thread: Problem adding two float values and converting it to a QString

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem adding two float values and converting it to a QString

    My goal is to have the user input a value (stored in the amount variable), add the value to the total variable and then output the total to a label. The value seems to be multiplied instead of added. When testing with the value of 1, total is automatically 2, 4, 6, etc. If anyone can please point out the problem, I would appreciate it. Also, this is my first Qt application so if there is a better way of doing this, I would love the advice. Thank you in advance.

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QWidget>
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9.  
    10.  
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private slots:
    22. void on_pushButton_clicked();
    23. private:
    24. Ui::MainWindow *ui;
    25. float amount;
    26. float total;
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. amount=0.00;
    10. total=0.00;
    11.  
    12. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20.  
    21. void MainWindow::on_pushButton_clicked()
    22. {
    23. amount = ui->lineEdit->text().toFloat();
    24. total += amount;
    25. ui->label->setText(QString::number(total, 'f', 2));
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem adding two float values and converting it to a QString

    on_pushButton_clicked() called twice
    read this - http://www.qtcentre.org/threads/2552...-on-pushbutton
    Just change on_pushButton_clicked to something else
    Last edited by folibis; 12th July 2012 at 06:49.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem adding two float values and converting it to a QString

    By naming the slot on_pushButton_clicked() the Designer UI code has automatically connected the clicked() signal of the pushButton object to it. You then manually connect the signal to the slot, resulting in two connections. Each time you press the button the slot is called twice.

  4. #4
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem adding two float values and converting it to a QString

    Problem solved . Thank you very much for your help.

Similar Threads

  1. Replies: 2
    Last Post: 22nd January 2015, 19:39
  2. converting QString to FLOAT behaves unexpectedly
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2011, 10:46
  3. Retrieving and converting values
    By jcoop in forum Newbie
    Replies: 3
    Last Post: 19th February 2009, 08:01
  4. QDoubleSpinBox and Float Values
    By George Neil in forum Qt Programming
    Replies: 9
    Last Post: 5th August 2008, 20:22
  5. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38

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.