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;

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    QMainWindow *mainWindow;
    QTabWidget *tabWidget;
    };

    class FirstTab : public QWidget
    {
    Q_OBJECT
    public:
    FirstTab(QWidget *parent = 0);
    };

    class SecondTab : public QWidget
    {
    Q_OBJECT
    public:
    SecondTab(QWidget *parent = 0);

    private slots:
    void calculate();

    private:
    QGroupBox *input();
    QGroupBox *resultbox();
    QGroupBox *calc();

     QLineEdit *input1;
     QLineEdit *input2;
     QLineEdit *output;
    

    };

    #endif // MAINWINDOW_H

    @

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include <QtGui>
    #include <QObject>
    #include <QTabWidget>
    #include <QLineEdit>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    tabWidget = new QTabWidget;
    tabWidget->addTab(new FirstTab(), tr("Start"));
    tabWidget->addTab(new SecondTab(), tr("App"));
    setCentralWidget(tabWidget);

    setMinimumSize(300, 300);
    setWindowTitle(tr("TabApp"));
    

    }

    FirstTab::FirstTab(QWidget *parent)
    : QWidget(parent)
    {
    QLabel *welcomeLabel = new QLabel(tr("Welcome to the first Tab!"));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(welcomeLabel);
    setLayout(mainLayout);
    

    }

    SecondTab::SecondTab(QWidget *parent)
    : QWidget(parent)
    {
    QGridLayout *grid = new QGridLayout;
    grid->addWidget(input(), 0, 0);
    grid->addWidget(resultbox(), 0, 1);
    grid->addWidget(calc(), 0, 2);
    setLayout(grid);
    }

    QGroupBox *SecondTab::input()
    {
    QGroupBox *groupBox = new QGroupBox(tr("Type two numbers"));

    QLabel *label1 = new QLabel(tr("Number 1:"));
    QLineEdit *input1 = new QLineEdit;
    QLabel *label2 = new QLabel(tr("Number 2:"));
    QLineEdit *input2 = new QLineEdit;
    
    QVBoxLayout *vbox = new QVBoxLayout;
     vbox->addWidget(label1);
     vbox->addWidget(input1);
     vbox->addWidget(label2);
     vbox->addWidget(input2);
     vbox->addStretch(1);
     groupBox->setLayout(vbox);
    
     return groupBox;
    

    }

    QGroupBox *SecondTab::resultbox()
    {
    QGroupBox *groupBox = new QGroupBox(tr("Result"));

    QLabel *resultlabel = new QLabel(tr("Result:"));
    QLineEdit *output = new QLineEdit;
    
    QVBoxLayout *vbox = new QVBoxLayout;
     vbox->addWidget(resultlabel);
     vbox->addWidget(output);
     vbox->addStretch(1);
     groupBox->setLayout(vbox);
    
     return groupBox;
    

    }

    QGroupBox *SecondTab::calc()
    {
    QGroupBox *groupBox = new QGroupBox(tr("Calculate"));
    QPushButton *pushButton = new QPushButton(tr("Calculate"));

    QVBoxLayout *vbox = new QVBoxLayout;
     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;

    QString resultString = "";
    output->setText(resultString.setNum(result));
    

    }

    MainWindow::~MainWindow()
    {
    delete mainWindow;
    }
    @

    If you see the mistake, please let me know.



  • 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:

    @
    QGroupBox *SecondTab::input()
    {
    QGroupBox *groupBox = new QGroupBox(tr("Type two numbers"));

    QLabel *label1 = new QLabel(tr("Number 1:"));
    input1 = new QLineEdit;
    QLabel *label2 = new QLabel(tr("Number 2:"));
    input2 = new QLineEdit;
    
    QVBoxLayout *vbox = new QVBoxLayout;
     vbox->addWidget(label1);
     vbox->addWidget(input1);
     vbox->addWidget(label2);
     vbox->addWidget(input2);
     vbox->addStretch(1);
     groupBox->setLayout(vbox);
    
     return groupBox;
    

    }

    QGroupBox *SecondTab::resultbox()
    {
    QGroupBox *groupBox = new QGroupBox(tr("Result"));

    QLabel *resultlabel = new QLabel(tr("Result:"));
    output = new QLineEdit;
    
    QVBoxLayout *vbox = new QVBoxLayout;
     vbox->addWidget(resultlabel);
     vbox->addWidget(output);
     vbox->addStretch(1);
     groupBox->setLayout(vbox);
    
     return groupBox;
    

    }

    @



  • Gerolf, thank you so much! It works and I understand what you explained! :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.