Okay, I have been working for a few hours and here is what I have so far. It is a bit different from what you had. For background, I created buttons in the mainwindow.ui and they are labeled as follows: lineEditA, lineEditB, and lineEditSum. The button I used was a tool button and I named it calculateButton. Instead of doing all of that original formula, I am just trying to do a simple a - b = sum now just so I can get the hang of that and then work up from there. I am getting some errors while running this code. I will explain the errors after I show the code.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
void onCalculateClicked();
void calculateAndDisplaySum();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
void onCalculateClicked();
void calculateAndDisplaySum();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include <QLineEdit>
#include <QToolButton>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->calculateButton, SIGNAL(clicked()), this, SLOT(onCalculateClicked()));
//onCalculateClicked(); -- Are these necessary?
//calculateAndDisplaySum();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onCalculateClicked()
{
calculateAndDisplaySum();
}
void MainWindow::calculateAndDisplaySum()
{
double a = lineEditA->text().toDouble();
double b = lineEditB->text().toDouble();
double sum = a - b;
lineEditSum
->setText
(QString::number(sum,
'g',
2));
}
#include <QLineEdit>
#include <QToolButton>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->calculateButton, SIGNAL(clicked()), this, SLOT(onCalculateClicked()));
//onCalculateClicked(); -- Are these necessary?
//calculateAndDisplaySum();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onCalculateClicked()
{
calculateAndDisplaySum();
}
void MainWindow::calculateAndDisplaySum()
{
double a = lineEditA->text().toDouble();
double b = lineEditB->text().toDouble();
double sum = a - b;
lineEditSum->setText(QString::number(sum, 'g', 2));
}
To copy to clipboard, switch view to plain text mode
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLineEdit" name="lineEditSum">
<property name="geometry">
<rect>
<x>120</x>
<y>10</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEditA">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEditB">
<property name="geometry">
<rect>
<x>60</x>
<y>10</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>10</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>100</x>
<y>10</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
<widget class="QToolButton" name="calculateButton">
<property name="geometry">
<rect>
<x>40</x>
<y>40</y>
<width>71</width>
<height>19</height>
</rect>
</property>
<property name="text">
<string> Calculate</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLineEdit" name="lineEditSum">
<property name="geometry">
<rect>
<x>120</x>
<y>10</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEditA">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEditB">
<property name="geometry">
<rect>
<x>60</x>
<y>10</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>10</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>100</x>
<y>10</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
<widget class="QToolButton" name="calculateButton">
<property name="geometry">
<rect>
<x>40</x>
<y>40</y>
<width>71</width>
<height>19</height>
</rect>
</property>
<property name="text">
<string> Calculate</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
To copy to clipboard, switch view to plain text mode
Okay, so now that's out of the way, just in case you want to compile it in QtCreator5.5 yourself, the error messages only show up on the mainwindow.cpp and they are as follows:
mainwindow.cpp:28: error: C2065: 'lineEditA' : undeclared identifier
mainwindow.cpp:28: error: C2227: left of '->text' must point to class/struct/union/generic type
type is 'unknown-type'
mainwindow.cpp:28: error: C2228: left of '.toDouble' must have class/struct/union
mainwindow.cpp:29: error: C2065: 'lineEditB' : undeclared identifier
mainwindow.cpp:29: error: C2227: left of '->text' must point to class/struct/union/generic type
type is 'unknown-type'
mainwindow.cpp:29: error: C2228: left of '.toDouble' must have class/struct/union
mainwindow.cpp:32: error: C2065: 'lineEditSum' : undeclared identifier
mainwindow.cpp:32: error: C2227: left of '->setText' must point to class/struct/union/generic type
type is 'unknown-type'
That should be it. Sorry for all the information and bombardment of code. Thank you for all the help!
Bookmarks