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. Performing Calculations with Data from LineEdits
Qt 6.11 is out! See what's new in the release blog

Performing Calculations with Data from LineEdits

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 330 Views
  • 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.
  • L Offline
    L Offline
    lukester88
    wrote last edited by
    #1

    Hello,

    I am struggling to understand the best way to take user input from LineEdits, and then perform calculations with them. I am creating a GUI that allows the user to fill out various QLineEdits with input values. I then want to perform a series of complex calculations with the values input in the QlineEdit boxes.

    I want to have the calculations done in one or more separate source files (that I would create), and NOT in the mainwindow.cpp source file. The problem is, I have no idea how I might access the values of QLineEdits and use those values in SEPARATE source files. In the code below, there are variables, R, gamma, inletT0 etc, that I wish to perform calculations with.

    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    #include <QDebug>
    #include <QDoubleValidator>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        // Set up user input fields with read-only format and double validator if they have a default/custom switch
        lineEdit_setup(ui->lineEdit_R, new QDoubleValidator(ui->lineEdit_R));
        lineEdit_setup(ui->lineEdit_gamma, new QDoubleValidator(ui->lineEdit_R));
        lineEdit_setup(ui->lineEdit_inletP0, new QDoubleValidator(ui->lineEdit_R));
        lineEdit_setup(ui->lineEdit_inletT0, new QDoubleValidator(ui->lineEdit_R));
    
        // Set up user input fields with just a double validator if they don't have a default/custom switch
        ui->lineEdit_desPR->setValidator(new QDoubleValidator);
        ui->lineEdit_desMdot->setValidator(new QDoubleValidator);
        ui->lineEdit_desN->setValidator(new QDoubleValidator);
    
        // Connect the signals of the Default/Custom comboBoxes with the slots of their respective lineEdits
        connect(ui->comboBox_R, &QComboBox::currentIndexChanged,
                ui->lineEdit_R, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_R, "53.346"); });
    
        connect(ui->comboBox_gamma, &QComboBox::currentIndexChanged,
                ui->lineEdit_gamma, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_gamma, "1.4"); });
    
        connect(ui->comboBox_inletT0, &QComboBox::currentIndexChanged,
                ui->lineEdit_inletT0, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_inletT0, "518.67"); });
    
        connect(ui->comboBox_inletP0, &QComboBox::currentIndexChanged,
                ui->lineEdit_inletP0, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_inletP0, "14.696"); });
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    // Defining a function to set format and read-only status depending on the Default or Custom value setting
    void MainWindow::comboBox_custom_input_activated(int index, QLineEdit *lineEdit, const QString &defaultText)
    {
        if (index == 0) // Default
        {
            lineEdit->setReadOnly(true);
            lineEdit->setText(defaultText);
            lineEdit->setStyleSheet("background-color: #E0E0E0; color: #7A7A7A");
        }
    
        if (index == 1)
        {
            lineEdit->setReadOnly(false);
            lineEdit->setStyleSheet("");
        }
    }
    
    // Defining a function to set up the user input fields with read-only format
    void MainWindow::lineEdit_setup(QLineEdit *lineEdit, QDoubleValidator *doubleValidator)
    {
        lineEdit->setReadOnly(true);
        lineEdit->setStyleSheet("background-color: #E0E0E0; color: #7A7A7A");
        lineEdit->setValidator(doubleValidator);
    }
    

    You will see various lineEdits defined in the provided mainwindow.cpp. It is from these lineEdits that I wish to take the inputs and perform calculations with them in a separate source file.

    For context, the combo boxes are used with some of the lineEdits to allow the user to input a custom value. By default, some of the QLineEdits are read-only and have default values in them, because the type of data that goes into those specific QLineEdits is usually the same regardless of the needs of the user. When the user starts up the program, some of the QLineEdits will be greyed out, but if the user selects the "custom" option in the combobox, they can enter their own value. I also have some functions that set format the QLineEdits depending on whether the combobox is set to default or custom. But I am not asking for help related to this aspect of the code, I just wanted to provide the background so people are clear on my intentions for the rest of the code that they may see when reading the post.

    Thank you in advance!

    JonBJ 1 Reply Last reply
    0
    • L lukester88

      Hello,

      I am struggling to understand the best way to take user input from LineEdits, and then perform calculations with them. I am creating a GUI that allows the user to fill out various QLineEdits with input values. I then want to perform a series of complex calculations with the values input in the QlineEdit boxes.

      I want to have the calculations done in one or more separate source files (that I would create), and NOT in the mainwindow.cpp source file. The problem is, I have no idea how I might access the values of QLineEdits and use those values in SEPARATE source files. In the code below, there are variables, R, gamma, inletT0 etc, that I wish to perform calculations with.

      #include "mainwindow.h"
      #include "./ui_mainwindow.h"
      #include <QDebug>
      #include <QDoubleValidator>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          // Set up user input fields with read-only format and double validator if they have a default/custom switch
          lineEdit_setup(ui->lineEdit_R, new QDoubleValidator(ui->lineEdit_R));
          lineEdit_setup(ui->lineEdit_gamma, new QDoubleValidator(ui->lineEdit_R));
          lineEdit_setup(ui->lineEdit_inletP0, new QDoubleValidator(ui->lineEdit_R));
          lineEdit_setup(ui->lineEdit_inletT0, new QDoubleValidator(ui->lineEdit_R));
      
          // Set up user input fields with just a double validator if they don't have a default/custom switch
          ui->lineEdit_desPR->setValidator(new QDoubleValidator);
          ui->lineEdit_desMdot->setValidator(new QDoubleValidator);
          ui->lineEdit_desN->setValidator(new QDoubleValidator);
      
          // Connect the signals of the Default/Custom comboBoxes with the slots of their respective lineEdits
          connect(ui->comboBox_R, &QComboBox::currentIndexChanged,
                  ui->lineEdit_R, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_R, "53.346"); });
      
          connect(ui->comboBox_gamma, &QComboBox::currentIndexChanged,
                  ui->lineEdit_gamma, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_gamma, "1.4"); });
      
          connect(ui->comboBox_inletT0, &QComboBox::currentIndexChanged,
                  ui->lineEdit_inletT0, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_inletT0, "518.67"); });
      
          connect(ui->comboBox_inletP0, &QComboBox::currentIndexChanged,
                  ui->lineEdit_inletP0, [this](int index) {comboBox_custom_input_activated(index, ui->lineEdit_inletP0, "14.696"); });
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      // Defining a function to set format and read-only status depending on the Default or Custom value setting
      void MainWindow::comboBox_custom_input_activated(int index, QLineEdit *lineEdit, const QString &defaultText)
      {
          if (index == 0) // Default
          {
              lineEdit->setReadOnly(true);
              lineEdit->setText(defaultText);
              lineEdit->setStyleSheet("background-color: #E0E0E0; color: #7A7A7A");
          }
      
          if (index == 1)
          {
              lineEdit->setReadOnly(false);
              lineEdit->setStyleSheet("");
          }
      }
      
      // Defining a function to set up the user input fields with read-only format
      void MainWindow::lineEdit_setup(QLineEdit *lineEdit, QDoubleValidator *doubleValidator)
      {
          lineEdit->setReadOnly(true);
          lineEdit->setStyleSheet("background-color: #E0E0E0; color: #7A7A7A");
          lineEdit->setValidator(doubleValidator);
      }
      

      You will see various lineEdits defined in the provided mainwindow.cpp. It is from these lineEdits that I wish to take the inputs and perform calculations with them in a separate source file.

      For context, the combo boxes are used with some of the lineEdits to allow the user to input a custom value. By default, some of the QLineEdits are read-only and have default values in them, because the type of data that goes into those specific QLineEdits is usually the same regardless of the needs of the user. When the user starts up the program, some of the QLineEdits will be greyed out, but if the user selects the "custom" option in the combobox, they can enter their own value. I also have some functions that set format the QLineEdits depending on whether the combobox is set to default or custom. But I am not asking for help related to this aspect of the code, I just wanted to provide the background so people are clear on my intentions for the rest of the code that they may see when reading the post.

      Thank you in advance!

      JonBJ Online
      JonBJ Online
      JonB
      wrote last edited by
      #2

      @lukester88
      In the MainWindow code, where you have a UI: take the text from the QLineEdits, parse it as required into a number (methods of QString; if you used QDoubleSpinBox instead of QLineEdit you could just get the value() and Qt does that for you, but that's a detail) and pass the resulting double as a parameter to whatever code you write elsewhere. The elsewhere knows nothing about there being a UI/MainWindow, it just sees numbers to do its work on.

      1 Reply Last reply
      3
      • L Offline
        L Offline
        lukester88
        wrote last edited by
        #3

        @JonB
        Thank you for the reply. I am not exactly sure how to implement your advice in my Qt project. Is it possible you could provide a simple example for me? Thank you.

        1 Reply Last reply
        0
        • JonBJ Online
          JonBJ Online
          JonB
          wrote last edited by
          #4
          QString text = ui->lineEdit()->text();
          double value = text.toDouble();
          callSomeOtherFunctionElsewhere(value);
          

          Don't know what your issue is.

          1 Reply Last reply
          2
          • Joe von HabsburgJ Offline
            Joe von HabsburgJ Offline
            Joe von Habsburg
            wrote last edited by Joe von Habsburg
            #5

            I think you should use a DoubleSpinBox instead of a LineEdit. But I’ll proceed as if you’re still using a LineEdit. You can configure it the same way within the DoubleSpinBox.

            First of all, I think you want to perform a joint calculation using all the values you have.

            To do this, you need to add a calculate function to the slots that are connected to the currentIndexChanged signal.

            As @JonB mentioned above, you can access those values.

            For example, if you have five parameters and three of them are used in one calculation while two are used in a different calculation , you’ll need to adapt and customize your function accordingly.

            QString r_text = ui->lineEdit_r->text();
            double r_double = r_text.toDouble();
            QString gamma_text = ui->lineEdit_gamma->text();
            double gamma_double = gamma_text .toDouble();
            QString inletP0_text = ui->lineEdit_inletP0->text();
            double inletP0_double = inletP0_text .toDouble();
            calculate1(r_double, gamma_double, inletP0);
            ...
            
            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