Results 1 to 7 of 7

Thread: Simple Math Calculation Questions in QT (PyQt, Qt4 C++)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Simple Math Calculation Questions in QT (PyQt, Qt4 C++)

    What I need to do is use data from 2 inputs (QLineEdit) and run this calculation in the background:

    a - b = c
    c * 2 = d / 0.9 = sum
    So if I understand you correctly, your user will type a number into each line edit (a and b), and when she is done, your program will run the two numbers through the equations above and display the result in another line edit? Where does "d" come from? Do you mean:

    d = c * 2
    sum = d / 0.9

    ??

    As far as retrieving the numbers from the line edits, that's simple (in C++; I don't know Python well enough to ensure my example would be correct):
    Qt Code:
    1. void MyDataEntryForm::calculateAndDisplaySum()
    2. {
    3. double a = lineEditA->text().toDouble();
    4. double b = lineEditB->text().toDouble();
    5. double sum = ((a - b) * 2.0) / 0.9;
    6. lineEditSum->setText( QString::number( sum, 'g', 2 ) ); // which will give a number like 'nn.nn'; change the 2 to something else for more decimals
    7. }
    To copy to clipboard, switch view to plain text mode 

    You have at least two options for getting the numbers from line edits A and B:

    - An Excel-like mode, where as soon as either number changes, line edit Sum gets updated
    - A "batch" mode, where the user has to click an "update" or "calculate" button before the sum gets updated

    If your users are used to Excel, this might be a more natural mode of use. If you implement the batch mode option, then you do nothing when the values are edited, but you connect the "clicked()" signal from your calculate pushbutton to a slot that executes the code above:

    Qt Code:
    1. MyDataEntryForm::MyDataEntryForm() : ui( new Ui::MyDataEntryForm )
    2. {
    3. ui->setupUi( this );
    4. connect( ui->calculateBtn, SIGNAL( clicked() ), this, SLOT( onCalculateClicked() ) );
    5. }
    6.  
    7. void MyDataEntryForm::onCalculateClicked()
    8. {
    9. calculateAndDisplaySum();
    10. }
    To copy to clipboard, switch view to plain text mode 

    If you use the Excel mode, then it is a bit more complicated, since you need to monitor the A and B line edits for changes and do the calculation dynamically:

    Qt Code:
    1. MyDataEntryForm::MyDataEntryForm() : ui( new Ui::MyDataEntryForm )
    2. {
    3. ui->setupUi( this );
    4. connect( ui->lineEditA, SIGNAL( editingFinished() ), this, SLOT( onEditingFinished ) );
    5. connect( ui->lineEditB, SIGNAL( editingFinished() ), this, SLOT( onEditingFinished ) );
    6. }
    7.  
    8. void MyDataEntryForm::onEditingFinished()
    9. {
    10. // We don't care which line edit changed, since we do the same thing for each of them
    11. calculateAndDisplaySum();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Now, none of this code has any error checking. What if the user enters "tater tots" instead of "42.0" in one of the line edits? So you could get fancier. If there are prescribed allowed ranges for A or B, you could install QDoubleValidator instances on each line edit to ensure that the user can't enter anything other than a valid number. If you install a validator, the editingFinished() signal will not be emitted unless a valid value is entered, so you can be assured that by the time it gets to your calculation, everything is valid.

    This will work with either interaction style - but you will need to manually check the validator state using the input text if you use the pushbutton style of interaction. I don't remember if the validator will allow an invalid entry to be made in the line edit; what I do remember is that the editingFinished() signal isn't emitted unless the input is valid, so this is a safer way to ensure correct inputs.

    As for your last question about converting a number in a range to a fixed value, if you have a predetermined set of ranges and outputs, the simplest way is just an if/else construct:

    Qt Code:
    1. double output = 0.0;
    2.  
    3. if ( 0.00 <= input && input < 0.20 ) // what if the input is between 0.19 and 0.20? Your equations don't cover those corner cases. This does.
    4. output = 5.0;
    5. else if ( 0.20 <= input && input < 0.50 )
    6. output = 10.0;
    7. else if ( 0.50 <= input && input < 0.80 )
    8. output = 15.0;
    9. else if ( 0.80 <= input && input < 1.10 )
    10. output = 20.0;
    11. else
    12. {
    13. // error, so take appropriate action
    14. }
    To copy to clipboard, switch view to plain text mode 

    Good luck.
    Last edited by d_stranz; 28th November 2015 at 18:50.

Similar Threads

  1. Few (hopefully) simple questions
    By QTGuy72 in forum Newbie
    Replies: 1
    Last Post: 10th July 2012, 17:30
  2. Simple questions
    By assismvla in forum Newbie
    Replies: 3
    Last Post: 2nd May 2010, 01:57
  3. may i ask questions related to pyqt here???
    By pyqt123 in forum Newbie
    Replies: 2
    Last Post: 14th December 2009, 06:28
  4. Two probably simple Qt4 Questions
    By frenk_castle in forum Newbie
    Replies: 3
    Last Post: 16th September 2009, 15:37
  5. Simple DB app, some questions.
    By juannm in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2008, 11:46

Tags for this Thread

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.