<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Performing Calculations with Data from LineEdits]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<pre><code>#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include &lt;QDebug&gt;
#include &lt;QDoubleValidator&gt;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    // Set up user input fields with read-only format and double validator if they have a default/custom switch
    lineEdit_setup(ui-&gt;lineEdit_R, new QDoubleValidator(ui-&gt;lineEdit_R));
    lineEdit_setup(ui-&gt;lineEdit_gamma, new QDoubleValidator(ui-&gt;lineEdit_R));
    lineEdit_setup(ui-&gt;lineEdit_inletP0, new QDoubleValidator(ui-&gt;lineEdit_R));
    lineEdit_setup(ui-&gt;lineEdit_inletT0, new QDoubleValidator(ui-&gt;lineEdit_R));

    // Set up user input fields with just a double validator if they don't have a default/custom switch
    ui-&gt;lineEdit_desPR-&gt;setValidator(new QDoubleValidator);
    ui-&gt;lineEdit_desMdot-&gt;setValidator(new QDoubleValidator);
    ui-&gt;lineEdit_desN-&gt;setValidator(new QDoubleValidator);

    // Connect the signals of the Default/Custom comboBoxes with the slots of their respective lineEdits
    connect(ui-&gt;comboBox_R, &amp;QComboBox::currentIndexChanged,
            ui-&gt;lineEdit_R, [this](int index) {comboBox_custom_input_activated(index, ui-&gt;lineEdit_R, "53.346"); });

    connect(ui-&gt;comboBox_gamma, &amp;QComboBox::currentIndexChanged,
            ui-&gt;lineEdit_gamma, [this](int index) {comboBox_custom_input_activated(index, ui-&gt;lineEdit_gamma, "1.4"); });

    connect(ui-&gt;comboBox_inletT0, &amp;QComboBox::currentIndexChanged,
            ui-&gt;lineEdit_inletT0, [this](int index) {comboBox_custom_input_activated(index, ui-&gt;lineEdit_inletT0, "518.67"); });

    connect(ui-&gt;comboBox_inletP0, &amp;QComboBox::currentIndexChanged,
            ui-&gt;lineEdit_inletP0, [this](int index) {comboBox_custom_input_activated(index, ui-&gt;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 &amp;defaultText)
{
    if (index == 0) // Default
    {
        lineEdit-&gt;setReadOnly(true);
        lineEdit-&gt;setText(defaultText);
        lineEdit-&gt;setStyleSheet("background-color: #E0E0E0; color: #7A7A7A");
    }

    if (index == 1)
    {
        lineEdit-&gt;setReadOnly(false);
        lineEdit-&gt;setStyleSheet("");
    }
}

// Defining a function to set up the user input fields with read-only format
void MainWindow::lineEdit_setup(QLineEdit *lineEdit, QDoubleValidator *doubleValidator)
{
    lineEdit-&gt;setReadOnly(true);
    lineEdit-&gt;setStyleSheet("background-color: #E0E0E0; color: #7A7A7A");
    lineEdit-&gt;setValidator(doubleValidator);
}
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">Thank you in advance!</p>
]]></description><link>https://forum.qt.io/topic/164793/performing-calculations-with-data-from-lineedits</link><generator>RSS for Node</generator><lastBuildDate>Tue, 30 Jun 2026 18:35:18 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164793.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 13 Jun 2026 22:17:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Performing Calculations with Data from LineEdits on Mon, 15 Jun 2026 14:46:13 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">First of all, I think you want to perform a joint calculation using all the values you have.</p>
<p dir="auto">To do this, you need to add a <code>calculate</code> function to the slots that are connected to the <code>currentIndexChanged</code> signal.</p>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> mentioned above, you can access those values.</p>
<p dir="auto">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.</p>
<pre><code>QString r_text = ui-&gt;lineEdit_r-&gt;text();
double r_double = r_text.toDouble();
QString gamma_text = ui-&gt;lineEdit_gamma-&gt;text();
double gamma_double = gamma_text .toDouble();
QString inletP0_text = ui-&gt;lineEdit_inletP0-&gt;text();
double inletP0_double = inletP0_text .toDouble();
calculate1(r_double, gamma_double, inletP0);
...
</code></pre>
]]></description><link>https://forum.qt.io/post/838752</link><guid isPermaLink="true">https://forum.qt.io/post/838752</guid><dc:creator><![CDATA[Joe von Habsburg]]></dc:creator><pubDate>Mon, 15 Jun 2026 14:46:13 GMT</pubDate></item><item><title><![CDATA[Reply to Performing Calculations with Data from LineEdits on Sun, 14 Jun 2026 18:19:56 GMT]]></title><description><![CDATA[<pre><code>QString text = ui-&gt;lineEdit()-&gt;text();
double value = text.toDouble();
callSomeOtherFunctionElsewhere(value);
</code></pre>
<p dir="auto">Don't know what your issue is.</p>
]]></description><link>https://forum.qt.io/post/838739</link><guid isPermaLink="true">https://forum.qt.io/post/838739</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 14 Jun 2026 18:19:56 GMT</pubDate></item><item><title><![CDATA[Reply to Performing Calculations with Data from LineEdits on Sun, 14 Jun 2026 17:21:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
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.</p>
]]></description><link>https://forum.qt.io/post/838738</link><guid isPermaLink="true">https://forum.qt.io/post/838738</guid><dc:creator><![CDATA[lukester88]]></dc:creator><pubDate>Sun, 14 Jun 2026 17:21:16 GMT</pubDate></item><item><title><![CDATA[Reply to Performing Calculations with Data from LineEdits on Sun, 14 Jun 2026 00:10:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukester88">@<bdi>lukester88</bdi></a><br />
In the <strong>MainWindow</strong> code, where you have a UI: take the text from the <code>QLineEdit</code>s, parse it as required into a number (methods of <code>QString</code>; if you used <code>QDoubleSpinBox</code> instead of <code>QLineEdit</code> you could just get the <code>value()</code> and Qt does that for you, but that's a detail) and pass the resulting <code>double</code> as a <em>parameter</em> 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.</p>
]]></description><link>https://forum.qt.io/post/838731</link><guid isPermaLink="true">https://forum.qt.io/post/838731</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 14 Jun 2026 00:10:00 GMT</pubDate></item></channel></rss>