March 15, 2011

newe1 newe1
Lab Rat
14 posts

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

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QTabWidget>
  6. #include <QDialog>
  7.  
  8.  
  9. class QTabWidget;
  10. class QWidget;
  11. class QLabel;
  12. class QGroupBox;
  13. class QLineEdit;
  14. class QPushButton;
  15.  
  16.  
  17. class MainWindow : public QMainWindow
  18. {
  19.     Q_OBJECT
  20.  
  21. public:
  22.     explicit MainWindow(QWidget *parent = 0);
  23.     ~MainWindow();
  24.  
  25. private:
  26.     QMainWindow *mainWindow;
  27.     QTabWidget *tabWidget;
  28. };
  29.  
  30.  
  31. class FirstTab : public QWidget
  32. {
  33.     Q_OBJECT
  34. public:
  35.     FirstTab(QWidget *parent = 0);
  36. };
  37.  
  38.  
  39. class SecondTab : public QWidget
  40. {
  41.     Q_OBJECT
  42. public:
  43.     SecondTab(QWidget *parent = 0);
  44.  
  45. private slots:
  46.     void calculate();
  47.  
  48. private:
  49.      QGroupBox *input();
  50.      QGroupBox *resultbox();
  51.      QGroupBox *calc();
  52.  
  53.      QLineEdit *input1;
  54.      QLineEdit *input2;
  55.      QLineEdit *output;
  56. };
  57.  
  58. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include <QtGui>
  3. #include <QObject>
  4. #include <QTabWidget>
  5. #include <QLineEdit>
  6.  
  7. MainWindow::MainWindow(QWidget *parent) :
  8.     QMainWindow(parent)
  9. {
  10.     tabWidget = new QTabWidget;
  11.     tabWidget->addTab(new FirstTab(), tr("Start"));
  12.     tabWidget->addTab(new SecondTab(), tr("App"));
  13.     setCentralWidget(tabWidget);
  14.  
  15.     setMinimumSize(300, 300);
  16.     setWindowTitle(tr("TabApp"));
  17. }
  18.  
  19.  
  20. FirstTab::FirstTab(QWidget *parent)
  21.     : QWidget(parent)
  22. {
  23.     QLabel *welcomeLabel = new QLabel(tr("Welcome to the first Tab!"));
  24.  
  25.     QVBoxLayout *mainLayout = new QVBoxLayout;
  26.     mainLayout->addWidget(welcomeLabel);
  27.     setLayout(mainLayout);
  28. }
  29.  
  30. SecondTab::SecondTab(QWidget *parent)
  31.     : QWidget(parent)
  32. {
  33.     QGridLayout *grid = new QGridLayout;
  34.     grid->addWidget(input(), 0, 0);
  35.     grid->addWidget(resultbox(), 0, 1);
  36.     grid->addWidget(calc(), 0, 2);
  37.        setLayout(grid);
  38. }
  39.  
  40. QGroupBox *SecondTab::input()
  41. {
  42.     QGroupBox *groupBox = new QGroupBox(tr("Type two numbers"));
  43.  
  44.     QLabel *label1 = new QLabel(tr("Number 1:"));
  45.     QLineEdit *input1 = new QLineEdit;
  46.     QLabel *label2 = new QLabel(tr("Number 2:"));
  47.     QLineEdit *input2 = new QLineEdit;
  48.  
  49.     QVBoxLayout *vbox = new QVBoxLayout;
  50.      vbox->addWidget(label1);
  51.      vbox->addWidget(input1);
  52.      vbox->addWidget(label2);
  53.      vbox->addWidget(input2);
  54.      vbox->addStretch(1);
  55.      groupBox->setLayout(vbox);
  56.  
  57.      return groupBox;
  58.  }
  59.  
  60. QGroupBox *SecondTab::resultbox()
  61. {
  62.     QGroupBox *groupBox = new QGroupBox(tr("Result"));
  63.  
  64.     QLabel *resultlabel = new QLabel(tr("Result:"));
  65.     QLineEdit *output = new QLineEdit;
  66.  
  67.     QVBoxLayout *vbox = new QVBoxLayout;
  68.      vbox->addWidget(resultlabel);
  69.      vbox->addWidget(output);
  70.      vbox->addStretch(1);
  71.      groupBox->setLayout(vbox);
  72.  
  73.      return groupBox;
  74. }
  75.  
  76. QGroupBox *SecondTab::calc()
  77. {
  78.     QGroupBox *groupBox = new QGroupBox(tr("Calculate"));
  79.     QPushButton *pushButton = new QPushButton(tr("Calculate"));
  80.  
  81.     QVBoxLayout *vbox = new QVBoxLayout;
  82.      vbox->addWidget(pushButton);
  83.      vbox->addStretch(1);
  84.      groupBox->setLayout(vbox);
  85.  
  86.      connect(pushButton, SIGNAL(clicked()),
  87.                 this, SLOT(calculate()));
  88.  
  89.      return groupBox;
  90.  }
  91.  
  92. void SecondTab::calculate()
  93. {
  94.     double result;
  95.     bool ok;
  96.     double number1 = input1->text().toInt(&ok,10);
  97.     double number2 = input2->text().toInt(&ok,10);
  98.     result = number1 + number2;
  99.  
  100.     QString resultString = "";
  101.     output->setText(resultString.setNum(result));
  102. }
  103.  
  104. MainWindow::~MainWindow()
  105. {
  106.     delete mainWindow;
  107. }

If you see the mistake, please let me know.

2 replies

March 15, 2011

Gerolf Gerolf
Mad Scientist
3005 posts

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:

  1. QGroupBox *SecondTab::input()
  2. {
  3.     QGroupBox *groupBox = new QGroupBox(tr("Type two numbers"));
  4.  
  5.     QLabel *label1 = new QLabel(tr("Number 1:"));
  6.     input1 = new QLineEdit;
  7.     QLabel *label2 = new QLabel(tr("Number 2:"));
  8.     input2 = new QLineEdit;
  9.  
  10.     QVBoxLayout *vbox = new QVBoxLayout;
  11.      vbox->addWidget(label1);
  12.      vbox->addWidget(input1);
  13.      vbox->addWidget(label2);
  14.      vbox->addWidget(input2);
  15.      vbox->addStretch(1);
  16.      groupBox->setLayout(vbox);
  17.  
  18.      return groupBox;
  19.  }
  20.  
  21. QGroupBox *SecondTab::resultbox()
  22. {
  23.     QGroupBox *groupBox = new QGroupBox(tr("Result"));
  24.  
  25.     QLabel *resultlabel = new QLabel(tr("Result:"));
  26.     output = new QLineEdit;
  27.  
  28.     QVBoxLayout *vbox = new QVBoxLayout;
  29.      vbox->addWidget(resultlabel);
  30.      vbox->addWidget(output);
  31.      vbox->addStretch(1);
  32.      groupBox->setLayout(vbox);
  33.  
  34.      return groupBox;
  35. }

 Signature 

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

March 15, 2011

newe1 newe1
Lab Rat
14 posts

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

 
  ‹‹ Using 3d models (.obj) AND materials (.mtl) in an opengl application      Tessellation QShader Support ››

You must log in to post a reply. Not a member yet? Register here!