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. Add two numbers

Add two numbers

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 7.0k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    newe1
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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;
      

      }

      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • N Offline
        N Offline
        newe1
        wrote on last edited by
        #3

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

        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