Qt-3-Programul-calculator: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Image:PIC201431734365429.jpg|Calculator]] | [[Image:http://www.planet-source-code.com/Upload_PSC/ScreenShots/PIC201431734365429.jpg|Calculator]] | ||
calculator3.cpp | calculator3.cpp | ||
<code>#include "calculator3.h&quot;<br />#include "ui_calculator3.h&quot; | |||
Calculator3::Calculator3(QWidget '''parent) :<br /> QWidget(parent),<br /> ui(new Ui::Calculator3)<br />{<br /> ui->setupUi(this); | |||
<br /> ui->comboBox->addItem("+", 0);<br /> ui->comboBox->addItem("<s>", 1);<br /> ui</s>>comboBox->addItem("'''", 2); | |||
ui->label_2->setNum(0);<br /> ui->label_2->setStyleSheet("QLabel { background-color: white}");<br /> ui->label_2->setMaximumWidth(25); | |||
connect(ui->comboBox, SIGNAL (activated(QString)), this, SLOT (calculate(QString)));<br /> connect(ui->spinBox, SIGNAL (valueChanged(int)), this, SLOT (update()));<br /> connect(ui->spinBox_2, SIGNAL (valueChanged(int)), this, SLOT (update())); | |||
} | |||
void Calculator3::calculate(QString string){<br /> if(string &quot;+&quot;)&#123; | |||
ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() + ui-&gt;spinBox_2-&gt;value()); | |||
&#125; | |||
if(string "<s>"){<br /> ui</s>>label_2->setNum(ui->spinBox->value() - ui->spinBox_2->value());<br /> }<br /> if(string == "'''"){<br /> ui->label_2->setNum(ui->spinBox->value()''' ui->spinBox_2->value());<br /> } | |||
} | |||
void Calculator3::update(){<br /> if(ui->comboBox->currentIndex()0)&#123; | |||
ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() + ui-&gt;spinBox_2-&gt;value()); | |||
&#125; | |||
if(ui-&gt;comboBox-&gt;currentIndex()1){<br /> ui->label_2->setNum(ui->spinBox->value() - ui->spinBox_2->value());<br /> }<br /> if(ui->comboBox->currentIndex()==2){<br /> ui->label_2->setNum(ui->spinBox->value() * ui->spinBox_2->value());<br /> }<br />} | |||
Calculator3::~Calculator3()<br />{<br /> delete ui;<br />}</code> | |||
calculator3.h | calculator3.h | ||
<code>#ifndef CALCULATOR3_H<br />#define CALCULATOR3_H | |||
#include <QWidget&gt;<br />#include <QSpinBox&gt;<br />#include <QComboBox&gt;<br />#include <QLabel&gt; | |||
namespace Ui {<br />class Calculator3;<br />} | |||
class Calculator3 : public QWidget<br />{<br /> Q_OBJECT | |||
public:<br /> explicit Calculator3(QWidget *parent = 0);<br /> ~Calculator3(); | |||
private:<br /> Ui::Calculator3 *ui;<br /> QSpinBox *spinBox;<br /> QSpinBox *spinBox_2;<br /> QComboBox *comboBox;<br /> QLabel *label_2; | |||
private slots:<br /> void calculate(QString string);<br /> void update(); | |||
}; | |||
#endif // CALCULATOR3_H<br /></code> | |||
main.cpp | main.cpp | ||
<code>#include "calculator3.h&quot;<br />#include <QApplication&gt; | |||
int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> Calculator3 w;<br /> w.show(); | |||
return a.exec&amp;#40;&#41;;<br />}<br /></code> | |||
calculator3.ui | calculator3.ui | ||
<code>&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Calculator3&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Calculator3&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;400&lt;/width&gt;<br /> <height&gt;300&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Calculator3&lt;/string&gt;<br /> </property&gt;<br /> <widget class="QWidget&quot; name=""><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;20&lt;/x&gt;<br /> <y&gt;30&lt;/y&gt;<br /> <width&gt;351&lt;/width&gt;<br /> <height&gt;29&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout&quot;><br /> <item&gt;<br /> <widget class="QSpinBox&quot; name="spinBox&quot;><br /> <property name="minimum&quot;><br /> <number&gt;–999&lt;/number&gt;<br /> </property&gt;<br /> <property name="maximum&quot;><br /> <number&gt;999&lt;/number&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QComboBox&quot; name="comboBox&quot;/&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QSpinBox&quot; name="spinBox_2&quot;><br /> <property name="minimum&quot;><br /> <number&gt;–999&lt;/number&gt;<br /> </property&gt;<br /> <property name="maximum&quot;><br /> <number&gt;999&lt;/number&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label&quot;><br /> <property name="text&quot;><br /> <string&gt;=</string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_2&quot;><br /> <property name="text&quot;><br /> <string&gt;TextLabel&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> </widget&gt;<br /> <layoutdefault spacing="6&quot; margin="11&quot;/&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&gt; |
Revision as of 11:40, 24 February 2015
calculator3.cpp
#include "calculator3.h&quot;<br />#include "ui_calculator3.h&quot;
Calculator3::Calculator3(QWidget '''parent) :<br /> QWidget(parent),<br /> ui(new Ui::Calculator3)<br />{<br /> ui->setupUi(this);
<br /> ui->comboBox->addItem("+", 0);<br /> ui->comboBox->addItem("<s>", 1);<br /> ui</s>>comboBox->addItem("'''", 2);
ui->label_2->setNum(0);<br /> ui->label_2->setStyleSheet("QLabel { background-color: white}");<br /> ui->label_2->setMaximumWidth(25);
connect(ui->comboBox, SIGNAL (activated(QString)), this, SLOT (calculate(QString)));<br /> connect(ui->spinBox, SIGNAL (valueChanged(int)), this, SLOT (update()));<br /> connect(ui->spinBox_2, SIGNAL (valueChanged(int)), this, SLOT (update()));
}
void Calculator3::calculate(QString string){<br /> if(string &quot;+&quot;)&#123;
ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() + ui-&gt;spinBox_2-&gt;value());
&#125;
if(string "<s>"){<br /> ui</s>>label_2->setNum(ui->spinBox->value() - ui->spinBox_2->value());<br /> }<br /> if(string == "'''"){<br /> ui->label_2->setNum(ui->spinBox->value()''' ui->spinBox_2->value());<br /> }
}
void Calculator3::update(){<br /> if(ui->comboBox->currentIndex()0)&#123;
ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() + ui-&gt;spinBox_2-&gt;value());
&#125;
if(ui-&gt;comboBox-&gt;currentIndex()1){<br /> ui->label_2->setNum(ui->spinBox->value() - ui->spinBox_2->value());<br /> }<br /> if(ui->comboBox->currentIndex()==2){<br /> ui->label_2->setNum(ui->spinBox->value() * ui->spinBox_2->value());<br /> }<br />}
Calculator3::~Calculator3()<br />{<br /> delete ui;<br />}
calculator3.h
#ifndef CALCULATOR3_H<br />#define CALCULATOR3_H
#include <QWidget&gt;<br />#include <QSpinBox&gt;<br />#include <QComboBox&gt;<br />#include <QLabel&gt;
namespace Ui {<br />class Calculator3;<br />}
class Calculator3 : public QWidget<br />{<br /> Q_OBJECT
public:<br /> explicit Calculator3(QWidget *parent = 0);<br /> ~Calculator3();
private:<br /> Ui::Calculator3 *ui;<br /> QSpinBox *spinBox;<br /> QSpinBox *spinBox_2;<br /> QComboBox *comboBox;<br /> QLabel *label_2;
private slots:<br /> void calculate(QString string);<br /> void update();
};
#endif // CALCULATOR3_H<br />
main.cpp
#include "calculator3.h&quot;<br />#include <QApplication&gt;
int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> Calculator3 w;<br /> w.show();
return a.exec&amp;#40;&#41;;<br />}<br />
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>