Qt-3-Programul-calculator

From Qt Wiki
Revision as of 11:58, 25 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search

Calculator

calculator3.cpp

#include "calculator3.h"
#include "ui_calculator3.h"

Calculator3::Calculator3(QWidget '''parent) :
 QWidget(parent),
 ui(new Ui::Calculator3)
{
 ui->setupUi(this);

 ui->comboBox->addItem("+", 0);
 ui->comboBox->addItem("-", 1);
 ui->comboBox->addItem("'''", 2);

ui->label_2->setNum(0);
 ui->label_2->setStyleSheet("QLabel { background-color: white}");
 ui->label_2->setMaximumWidth(25);

connect(ui->comboBox, SIGNAL (activated(QString)), this, SLOT (calculate(QString)));
 connect(ui->spinBox, SIGNAL (valueChanged(int)), this, SLOT (update()));
 connect(ui->spinBox_2, SIGNAL (valueChanged(int)), this, SLOT (update()));

}

void Calculator3::calculate(QString string){
 if(string  "+"){
        ui->label_2->setNum(ui->spinBox->value() + ui->spinBox_2->value());
    }
    if(string  "-"){
 ui->label_2->setNum(ui->spinBox->value() - ui->spinBox_2->value());
 }
 if(string == "'''"){
 ui->label_2->setNum(ui->spinBox->value()''' ui->spinBox_2->value());
 }

}

void Calculator3::update(){
 if(ui->comboBox->currentIndex()0){
        ui->label_2->setNum(ui->spinBox->value() + ui->spinBox_2->value());
    }
    if(ui->comboBox->currentIndex()1){
 ui->label_2->setNum(ui->spinBox->value() - ui->spinBox_2->value());
 }
 if(ui->comboBox->currentIndex()==2){
 ui->label_2->setNum(ui->spinBox->value() * ui->spinBox_2->value());
 }
}

Calculator3::~Calculator3()
{
 delete ui;
}

calculator3.h

#ifndef CALCULATOR3_H
#define CALCULATOR3_H

#include <QWidget>
#include <QSpinBox>
#include <QComboBox>
#include <QLabel>

namespace Ui {
class Calculator3;
}

class Calculator3 : public QWidget
{
 Q_OBJECT

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

private:
 Ui::Calculator3 *ui;
 QSpinBox *spinBox;
 QSpinBox *spinBox_2;
 QComboBox *comboBox;
 QLabel *label_2;

private slots:
 void calculate(QString string);
 void update();

};

#endif // CALCULATOR3_H

main.cpp

#include "calculator3.h"
#include <QApplication>

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 Calculator3 w;
 w.show();

return a.exec();
}

calculator3.ui

<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0">

<class>Calculator3</class>
<widget class="QWidget" name="Calculator3">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator3</string>
</property>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>351</width>
<height>29</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QSpinBox" name="spinBox">
<property name="minimum">
<number>–999</number>
</property>
<property name="maximum">
<number>999</number>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox"/>
</item>
<item>
<widget class="QSpinBox" name="spinBox_2">
<property name="minimum">
<number>–999</number>
</property>
<property name="maximum">
<number>999</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>

</ui>