Qt-3-Programul-calculator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
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 &quot;calculator3.h&amp;quot;<br />#include &quot;ui_calculator3.h&amp;quot;
Calculator3::Calculator3(QWidget '''parent) :<br /> QWidget(parent),<br /> ui(new Ui::Calculator3)<br />{<br /> ui-&gt;setupUi(this);
<br /> ui-&gt;comboBox-&gt;addItem(&quot;+&quot;, 0);<br /> ui-&gt;comboBox-&gt;addItem(&quot;<s>&quot;, 1);<br /> ui</s>&gt;comboBox-&gt;addItem(&quot;'''&quot;, 2);
ui-&gt;label_2-&gt;setNum(0);<br /> ui-&gt;label_2-&gt;setStyleSheet(&quot;QLabel { background-color: white}&quot;);<br /> ui-&gt;label_2-&gt;setMaximumWidth(25);
connect(ui-&gt;comboBox, SIGNAL (activated(QString)), this, SLOT (calculate(QString)));<br /> connect(ui-&gt;spinBox, SIGNAL (valueChanged(int)), this, SLOT (update()));<br /> connect(ui-&gt;spinBox_2, SIGNAL (valueChanged(int)), this, SLOT (update()));
}
void Calculator3::calculate(QString string){<br /> if(string  &amp;quot;+&amp;quot;)&amp;#123;
        ui-&amp;gt;label_2-&amp;gt;setNum(ui-&amp;gt;spinBox-&amp;gt;value() + ui-&amp;gt;spinBox_2-&amp;gt;value());
    &amp;#125;
    if(string  &quot;<s>&quot;){<br /> ui</s>&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() - ui-&gt;spinBox_2-&gt;value());<br /> }<br /> if(string == &quot;'''&quot;){<br /> ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value()''' ui-&gt;spinBox_2-&gt;value());<br /> }
}
void Calculator3::update(){<br /> if(ui-&gt;comboBox-&gt;currentIndex()0)&amp;#123;
        ui-&amp;gt;label_2-&amp;gt;setNum(ui-&amp;gt;spinBox-&amp;gt;value() + ui-&amp;gt;spinBox_2-&amp;gt;value());
    &amp;#125;
    if(ui-&amp;gt;comboBox-&amp;gt;currentIndex()1){<br /> ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() - ui-&gt;spinBox_2-&gt;value());<br /> }<br /> if(ui-&gt;comboBox-&gt;currentIndex()==2){<br /> ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() * ui-&gt;spinBox_2-&gt;value());<br /> }<br />}
Calculator3::~Calculator3()<br />{<br /> delete ui;<br />}</code>


calculator3.h
calculator3.h
<code>#ifndef CALCULATOR3_H<br />#define CALCULATOR3_H
#include &lt;QWidget&amp;gt;<br />#include &lt;QSpinBox&amp;gt;<br />#include &lt;QComboBox&amp;gt;<br />#include &lt;QLabel&amp;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 &quot;calculator3.h&amp;quot;<br />#include &lt;QApplication&amp;gt;
int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> Calculator3 w;<br /> w.show();
return a.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>


calculator3.ui
calculator3.ui
<code>&amp;lt;?xml version=&quot;1.0&amp;quot; encoding=&quot;UTF-8&amp;quot;?&amp;gt;<br />&lt;ui version=&quot;4.0&amp;quot;&gt;<br /> &lt;class&amp;gt;Calculator3&amp;lt;/class&amp;gt;<br /> &lt;widget class=&quot;QWidget&amp;quot; name=&quot;Calculator3&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;0&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;0&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;400&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;300&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;windowTitle&amp;quot;&gt;<br /> &lt;string&amp;gt;Calculator3&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;widget class=&quot;QWidget&amp;quot; name=&quot;&quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;20&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;30&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;351&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;29&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;layout class=&quot;QHBoxLayout&amp;quot; name=&quot;horizontalLayout&amp;quot;&gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QSpinBox&amp;quot; name=&quot;spinBox&amp;quot;&gt;<br /> &lt;property name=&quot;minimum&amp;quot;&gt;<br /> &lt;number&amp;gt;–999&amp;lt;/number&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;maximum&amp;quot;&gt;<br /> &lt;number&amp;gt;999&amp;lt;/number&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QComboBox&amp;quot; name=&quot;comboBox&amp;quot;/&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QSpinBox&amp;quot; name=&quot;spinBox_2&amp;quot;&gt;<br /> &lt;property name=&quot;minimum&amp;quot;&gt;<br /> &lt;number&amp;gt;–999&amp;lt;/number&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;maximum&amp;quot;&gt;<br /> &lt;number&amp;gt;999&amp;lt;/number&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QLabel&amp;quot; name=&quot;label&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;=&lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;item&amp;gt;<br /> &lt;widget class=&quot;QLabel&amp;quot; name=&quot;label_2&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;TextLabel&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;/layout&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;layoutdefault spacing=&quot;6&amp;quot; margin=&quot;11&amp;quot;/&amp;gt;<br /> &lt;resources/&amp;gt;<br /> &lt;connections/&amp;gt;<br />&lt;/ui&amp;gt;

Revision as of 11:40, 24 February 2015

Calculator

calculator3.cpp

#include &quot;calculator3.h&amp;quot;<br />#include &quot;ui_calculator3.h&amp;quot;

Calculator3::Calculator3(QWidget '''parent) :<br /> QWidget(parent),<br /> ui(new Ui::Calculator3)<br />{<br /> ui-&gt;setupUi(this);
<br /> ui-&gt;comboBox-&gt;addItem(&quot;+&quot;, 0);<br /> ui-&gt;comboBox-&gt;addItem(&quot;<s>&quot;, 1);<br /> ui</s>&gt;comboBox-&gt;addItem(&quot;'''&quot;, 2);

ui-&gt;label_2-&gt;setNum(0);<br /> ui-&gt;label_2-&gt;setStyleSheet(&quot;QLabel { background-color: white}&quot;);<br /> ui-&gt;label_2-&gt;setMaximumWidth(25);

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

}

void Calculator3::calculate(QString string){<br /> if(string  &amp;quot;+&amp;quot;)&amp;#123;
        ui-&amp;gt;label_2-&amp;gt;setNum(ui-&amp;gt;spinBox-&amp;gt;value() + ui-&amp;gt;spinBox_2-&amp;gt;value());
    &amp;#125;
    if(string  &quot;<s>&quot;){<br /> ui</s>&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() - ui-&gt;spinBox_2-&gt;value());<br /> }<br /> if(string == &quot;'''&quot;){<br /> ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value()''' ui-&gt;spinBox_2-&gt;value());<br /> }

}

void Calculator3::update(){<br /> if(ui-&gt;comboBox-&gt;currentIndex()0)&amp;#123;
        ui-&amp;gt;label_2-&amp;gt;setNum(ui-&amp;gt;spinBox-&amp;gt;value() + ui-&amp;gt;spinBox_2-&amp;gt;value());
    &amp;#125;
    if(ui-&amp;gt;comboBox-&amp;gt;currentIndex()1){<br /> ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() - ui-&gt;spinBox_2-&gt;value());<br /> }<br /> if(ui-&gt;comboBox-&gt;currentIndex()==2){<br /> ui-&gt;label_2-&gt;setNum(ui-&gt;spinBox-&gt;value() * ui-&gt;spinBox_2-&gt;value());<br /> }<br />}

Calculator3::~Calculator3()<br />{<br /> delete ui;<br />}

calculator3.h

#ifndef CALCULATOR3_H<br />#define CALCULATOR3_H

#include &lt;QWidget&amp;gt;<br />#include &lt;QSpinBox&amp;gt;<br />#include &lt;QComboBox&amp;gt;<br />#include &lt;QLabel&amp;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 &quot;calculator3.h&amp;quot;<br />#include &lt;QApplication&amp;gt;

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

return a.exec&amp;amp;#40;&amp;#41;;<br />}<br />

calculator3.ui

&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;
<ui version="4.0&quot;>
<class&gt;Calculator3&lt;/class&gt;
<widget class="QWidget&quot; name="Calculator3&quot;>
<property name="geometry&quot;>
<rect&gt;
<x&gt;0&lt;/x&gt;
<y&gt;0&lt;/y&gt;
<width&gt;400&lt;/width&gt;
<height&gt;300&lt;/height&gt;
</rect&gt;
</property&gt;
<property name="windowTitle&quot;>
<string&gt;Calculator3&lt;/string&gt;
</property&gt;
<widget class="QWidget&quot; name="">
<property name="geometry&quot;>
<rect&gt;
<x&gt;20&lt;/x&gt;
<y&gt;30&lt;/y&gt;
<width&gt;351&lt;/width&gt;
<height&gt;29&lt;/height&gt;
</rect&gt;
</property&gt;
<layout class="QHBoxLayout&quot; name="horizontalLayout&quot;>
<item&gt;
<widget class="QSpinBox&quot; name="spinBox&quot;>
<property name="minimum&quot;>
<number&gt;–999&lt;/number&gt;
</property&gt;
<property name="maximum&quot;>
<number&gt;999&lt;/number&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<widget class="QComboBox&quot; name="comboBox&quot;/&gt;
</item&gt;
<item&gt;
<widget class="QSpinBox&quot; name="spinBox_2&quot;>
<property name="minimum&quot;>
<number&gt;–999&lt;/number&gt;
</property&gt;
<property name="maximum&quot;>
<number&gt;999&lt;/number&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<widget class="QLabel&quot; name="label&quot;>
<property name="text&quot;>
<string&gt;=</string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<widget class="QLabel&quot; name="label_2&quot;>
<property name="text&quot;>
<string&gt;TextLabel&lt;/string&gt;
</property&gt;
</widget&gt;
</item&gt;
</layout&gt;
</widget&gt;
</widget&gt;
<layoutdefault spacing="6&quot; margin="11&quot;/&gt;
<resources/&gt;
<connections/&gt;
</ui&gt;