Qt-3-Programul-calculator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Add "cleanup" tag)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Image:http://www.planet-source-code.com/Upload_PSC/ScreenShots/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;
<code>#include "calculator3.h"
#include "ui_calculator3.h"


Calculator3::Calculator3(QWidget '''parent) :<br /> QWidget(parent),<br /> ui(new Ui::Calculator3)<br />{<br /> ui-&gt;setupUi(this);
Calculator3::Calculator3(QWidget '''parent) :
<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);
QWidget(parent),
ui(new Ui::Calculator3)
{
ui->setupUi(this);


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);
ui->comboBox->addItem("+", 0);
ui->comboBox->addItem("-", 1);
ui->comboBox->addItem("'''", 2);


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()));
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){<br /> if(string  &amp;quot;+&amp;quot;)&amp;#123;
void Calculator3::calculate(QString string){
         ui-&amp;gt;label_2-&amp;gt;setNum(ui-&amp;gt;spinBox-&amp;gt;value() + ui-&amp;gt;spinBox_2-&amp;gt;value());
if(string  "+"){
     &amp;#125;
         ui->label_2->setNum(ui->spinBox->value() + ui->spinBox_2->value());
     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 /> }
     }
     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(){<br /> if(ui-&gt;comboBox-&gt;currentIndex()0)&amp;#123;
void Calculator3::update(){
         ui-&amp;gt;label_2-&amp;gt;setNum(ui-&amp;gt;spinBox-&amp;gt;value() + ui-&amp;gt;spinBox_2-&amp;gt;value());
if(ui->comboBox->currentIndex()0){
     &amp;#125;
         ui->label_2->setNum(ui->spinBox->value() + ui->spinBox_2->value());
     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 />}
     }
     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()<br />{<br /> delete ui;<br />}</code>
Calculator3::~Calculator3()
{
delete ui;
}</code>


calculator3.h
calculator3.h


<code>#ifndef CALCULATOR3_H<br />#define CALCULATOR3_H
<code>#ifndef CALCULATOR3_H
#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;
#include <QWidget>
#include <QSpinBox>
#include <QComboBox>
#include <QLabel>


namespace Ui {<br />class Calculator3;<br />}
namespace Ui {
class Calculator3;
}


class Calculator3 : public QWidget<br />{<br /> Q_OBJECT
class Calculator3 : public QWidget
{
Q_OBJECT


public:<br /> explicit Calculator3(QWidget *parent = 0);<br /> ~Calculator3();
public:
explicit Calculator3(QWidget *parent = 0);
~Calculator3();


private:<br /> Ui::Calculator3 *ui;<br /> QSpinBox *spinBox;<br /> QSpinBox *spinBox_2;<br /> QComboBox *comboBox;<br /> QLabel *label_2;
private:
Ui::Calculator3 *ui;
QSpinBox *spinBox;
QSpinBox *spinBox_2;
QComboBox *comboBox;
QLabel *label_2;


private slots:<br /> void calculate(QString string);<br /> void update();
private slots:
void calculate(QString string);
void update();


};
};


#endif // CALCULATOR3_H<br /></code>
#endif // CALCULATOR3_H
</code>


main.cpp
main.cpp


<code>#include &quot;calculator3.h&amp;quot;<br />#include &lt;QApplication&amp;gt;
<code>#include "calculator3.h"
#include <QApplication>


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


return a.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
return a.exec();
}
</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;
<code><?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>

Latest revision as of 16:25, 3 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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>