Add two numbers
Hi!
I just want to add two numbers from the 1st groupBox (input) and display the result in the 2nd groupBox (output). Well everything is displayed, but the calculation is not done.
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QTabWidget>
- #include <QDialog>
- class QTabWidget;
- class QWidget;
- class QLabel;
- class QGroupBox;
- class QLineEdit;
- class QPushButton;
- {
- Q_OBJECT
- public:
- ~MainWindow();
- private:
- };
- {
- Q_OBJECT
- public:
- };
- {
- Q_OBJECT
- public:
- private slots:
- void calculate();
- private:
- };
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include "mainwindow.h"
- #include <QtGui>
- #include <QObject>
- #include <QTabWidget>
- #include <QLineEdit>
- {
- tabWidget = new QTabWidget;
- tabWidget->addTab(new FirstTab(), tr("Start"));
- tabWidget->addTab(new SecondTab(), tr("App"));
- setCentralWidget(tabWidget);
- setMinimumSize(300, 300);
- setWindowTitle(tr("TabApp"));
- }
- {
- mainLayout->addWidget(welcomeLabel);
- setLayout(mainLayout);
- }
- {
- grid->addWidget(input(), 0, 0);
- grid->addWidget(resultbox(), 0, 1);
- grid->addWidget(calc(), 0, 2);
- setLayout(grid);
- }
- {
- vbox->addWidget(label1);
- vbox->addWidget(input1);
- vbox->addWidget(label2);
- vbox->addWidget(input2);
- vbox->addStretch(1);
- groupBox->setLayout(vbox);
- return groupBox;
- }
- {
- vbox->addWidget(resultlabel);
- vbox->addWidget(output);
- vbox->addStretch(1);
- groupBox->setLayout(vbox);
- return groupBox;
- }
- {
- vbox->addWidget(pushButton);
- vbox->addStretch(1);
- groupBox->setLayout(vbox);
- connect(pushButton, SIGNAL(clicked()),
- this, SLOT(calculate()));
- return groupBox;
- }
- void SecondTab::calculate()
- {
- double result;
- bool ok;
- double number1 = input1->text().toInt(&ok,10);
- double number2 = input2->text().toInt(&ok,10);
- result = number1 + number2;
- output->setText(resultString.setNum(result));
- }
- MainWindow::~MainWindow()
- {
- delete mainWindow;
- }
If you see the mistake, please let me know.
2 replies
Hi,
the problem is clear:
in your creator function (like SecondTab::resultbox())
you ceate local QLineEdit* variables here you set the pointers. If you set the members in the C#tor to 0, you will get o-pointer access. use the members in the creator functions and it will work, like here:
- {
- input1 = new QLineEdit;
- input2 = new QLineEdit;
- vbox->addWidget(label1);
- vbox->addWidget(input1);
- vbox->addWidget(label2);
- vbox->addWidget(input2);
- vbox->addStretch(1);
- groupBox->setLayout(vbox);
- return groupBox;
- }
- {
- output = new QLineEdit;
- vbox->addWidget(resultlabel);
- vbox->addWidget(output);
- vbox->addStretch(1);
- groupBox->setLayout(vbox);
- return groupBox;
- }
You must log in to post a reply. Not a member yet? Register here!


