×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Como criar uma calculadora simples, em Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update of Template:ArticleMetaData)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(10 intermediate revisions by one user not shown)
Line 1: Line 1:
[[Category:Lang-Portuguese]]
+
{{Archived|language=Lang-Portuguese|timestamp=20120216044749|user=[[User:Hamishwillee|&lt;br /&gt;----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (based on {{Qapiname|QWidget}}) is deprecated.}}
[[Category:Qt]]
+
[[Category:Lang-Portuguese]][[Category:Qt C++ UI]][[Category:Code Examples]][[Category:MeeGo Harmattan]][[Category:Symbian]]
[[Category:Code Examples]]
+
{{Abstract|Este trecho de código demonstra como criar uma simples calculadora usando [[Qt (Portuguese)|Qt]].}}
__NOTOC__
+
{{ArticleMetaData <!-- v1.2 -->
__NOEDITSECTION__
+
{{ArticleMetaData
+
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Emulator
 
|devices= Emulator
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition  
+
|platform= S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
+
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords=QString, Push Button, Text Label
+
|keywords= QString, Push Button, Text Label
|id=...
+
|language= Lang-Portuguese
|language=Lang-Portuguese
+
|translated-by= [[User:Valderind4]]
|translated-by=[[User:Valderind4]]
+
|translated-from-title= Archived:Creating a simple calculator using QWidgets
|translated-from-title=Creating a simple calculator in Qt for Symbian
+
|translated-from-id= 60532 <!-- automated guess -->
|translated-from-id=60532 <!-- automated guess -->
+
|review-by= <!-- After re-review: [[User:username]] -->
|review-by=<!-- After re-review: [[User:username]] -->
+
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
|creationdate=20090912
+
|creationdate= 20090912
|author=[[User:Kiran10182]]
+
|author= [[User:Kiran10182]]
 
+
<!-- The following items are not in the standard metadata template -->
+
|category=Qt for Symbian
+
|subcategory=Application
+
 
}}
 
}}
 
 
==Introdução==
 
 
Este trecho de código demonstra como criar uma simples calculadora usando [[Qt for Symbian]].
 
 
==Pré-requisitos==
 
 
* Baixe e instale a versão atual do Qt para Symbian segundo as instruções [[Qt para Symbian|deste artigo]].
 
 
== Design do Layout ==
 
 
  
 
== Arquivo de cabeçalho ==
 
== Arquivo de cabeçalho ==
 
=== Cal.h===
 
=== Cal.h===
<code cpp>
+
<code cpp-qt>
  
 
#ifndef CAL_H
 
#ifndef CAL_H
Line 96: Line 77:
 
== Código fonte ==
 
== Código fonte ==
 
===cal.cpp===
 
===cal.cpp===
<code cpp>
+
<code cpp-qt>
  
 
#include "cal.h"
 
#include "cal.h"
Line 193: Line 174:
 
*Entrada de algum valor na linha de edição.
 
*Entrada de algum valor na linha de edição.
  
[[Image:Cal001.jpg]]
+
[[File:Cal001.jpg]]
  
 
*Operação decimal também é possível na calculadora.
 
*Operação decimal também é possível na calculadora.
Line 202: Line 183:
 
== Design da UI ==
 
== Design da UI ==
 
=== QtCalculator.ui===
 
=== QtCalculator.ui===
[[Image:QtCalculatorUi.png‎]]
+
[[File:QtCalculatorUi.png‎]]
  
  
 
==Arquivo de cabeçalho==
 
==Arquivo de cabeçalho==
 
=== QtCalculator.h===
 
=== QtCalculator.h===
<code cpp>
+
<code cpp-qt>
 
#ifndef QTCALCULATOR_H
 
#ifndef QTCALCULATOR_H
 
#define QTCALCULATOR_H
 
#define QTCALCULATOR_H
Line 238: Line 219:
 
==Código fonte==
 
==Código fonte==
 
=== QtCalculator.cpp===
 
=== QtCalculator.cpp===
<code cpp>
+
<code cpp-qt>
 
#include "QtCalculator.h"
 
#include "QtCalculator.h"
  
Line 331: Line 312:
  
 
* Será mostrada um simples calculadora na tela.
 
* Será mostrada um simples calculadora na tela.
[[Image:QtCalculatorScreenShot.png]]
+
[[File:QtCalculatorScreenShot.png]]
  
 
== Exemplo ==
 
== Exemplo ==
 
* Faça o download do exemplo trabalhado neste link: [[Media:QtCalculator.zip|QtCalculator.zip]]
 
* Faça o download do exemplo trabalhado neste link: [[Media:QtCalculator.zip|QtCalculator.zip]]
 
  
 
== Feedback ==
 
== Feedback ==
* Se você econtrar algum problema na implementação desta simples calculadora, então por favor escreva seu ''feedback'' na página deste artigo <u>[[Talk:Creating a simple calculator in Qt for S60|Comment page]]</u> (Inglês). Eu tentarei dá o máximo para tentar ajudar você.
+
* Se você econtrar algum problema na implementação desta simples calculadora, então por favor escreva seu ''feedback'' na página deste artigo <u>[[Talk:Creating a simple calculator in Qt|Comment page]]</u> (Inglês). Eu tentarei dá o máximo para tentar ajudar você.
  
 
==Comentário==
 
==Comentário==
 
Código de erro resolvido.
 
Código de erro resolvido.
<!-- Translation --> [[en:Creating a simple calculator in Qt for Symbian]]
+
<!-- Translation --> [[en:Archived:Creating a simple calculator using QWidgets]]

Latest revision as of 04:13, 11 October 2012

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

Este trecho de código demonstra como criar uma simples calculadora usando Qt.

Article Metadata

Testado com
Aparelho(s): Emulator

Compatibilidade
Plataforma(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian

Artigo
Palavras-chave: QString, Push Button, Text Label
Tradução:
Por valderind4
Última alteração feita por hamishwillee em 11 Oct 2012

Contents

[edit] Arquivo de cabeçalho

[edit] Cal.h

#ifndef CAL_H
#define CAL_H
 
#include <QtGui/QWidget>
#include "ui_cal.h"
#include<QString>
#include<QPushButton>
#include<QLineEdit>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QFormLayout>
class cal : public QWidget
{
Q_OBJECT
 
public:
cal(QWidget *parent = 0);
~cal();
 
private:
QPushButton *but1;
QPushButton *sub;
QPushButton *mul;
QPushButton *div;
QPushButton *eq;
QFormLayout *lay;
QLineEdit *line;
QString str;
QString str1;
QString ch;
QString strResult;
private slots:
void addi();
void subs();
void mult();
void divi();
void em();
public:
float num;
float num1;
float ans;
};
 
#endif // CAL_H

[edit] Código fonte

[edit] cal.cpp

#include "cal.h"
 
cal::cal(QWidget *parent)
: QWidget(parent)
{
lay=new QFormLayout(this);
but1=new QPushButton("+",this);
sub=new QPushButton("-",this);
mul=new QPushButton("*",this);
div=new QPushButton("/",this);
eq=new QPushButton("=",this);
line=new QLineEdit(this);
line->setMaxLength(12);
lay->addWidget(line);
lay->addWidget(but1)
lay->addWidget(sub);
lay->addWidget(mul);
lay->addWidget(div);
lay->addWidget(eq);
setLayout(lay);
QObject::connect(but1,SIGNAL(clicked()),this,SLOT(addi()));
QObject::connect(sub,SIGNAL(clicked()),this,SLOT(subs()));
QObject::connect(mul,SIGNAL(clicked()),this,SLOT(mult()));
QObject::connect(div,SIGNAL(clicked()),this,SLOT(divi()));
QObject::connect(eq,SIGNAL(clicked()),this,SLOT(em()));
}
 
cal::~cal()
{
// No need to delete any object that got a parent that is properly deleted.
 
}
void cal::addi()
{
str=line->text();
num=str.toFloat();
ch=but1->text();
line->clear();
}
void cal::subs()
{
str=line->text();
num=str.toFloat();
ch=sub->text();
line->clear();
}
void cal::mult()
{
str=line->text();
num=str.toFloat();
ch=mul->text();
line->clear();
}
void cal::divi()
{
str=line->text();
num=str.toFloat();
ch=div->text();
line->clear();
}
void cal::em()
{
 
 
str1 = line->text();
num1 = str1.toFloat();
if (ch=="+")
{
ans=num+num1;
}
else if(ch=="-")
{
ans=num-num1;
}
else if(ch=="*")
{
ans=num*num1;
}
else if(ch=="/")
{
ans=num/num1;
}
else
{
strResult = strResult.number(ans);
line->setText(strResult);
}
strResult = strResult.number(ans);
line->setText(strResult);
ch="";
}

[edit] Captura de tela

  • Entrada de algum valor na linha de edição.

Cal001.jpg

  • Operação decimal também é possível na calculadora.



[edit] Design da UI

[edit] QtCalculator.ui

QtCalculatorUi.png


[edit] Arquivo de cabeçalho

[edit] QtCalculator.h

#ifndef QTCALCULATOR_H
#define QTCALCULATOR_H
 
#include <QtGui/QWidget>
#include "ui_QtCalculator.h"
 
class QtCalculator : public QWidget
{
Q_OBJECT
 
public:
QtCalculator(QWidget *parent = 0);
~QtCalculator();
 
public slots:
void Addition();
void Subtraction();
void Multiplication();
void Division();
 
private:
Ui::QtCalculatorClass ui;
};
 
#endif // QTCALCULATOR_H


[edit] Código fonte

[edit] QtCalculator.cpp

#include "QtCalculator.h"
 
QtCalculator::QtCalculator(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
QObject::connect(ui.plusButton, SIGNAL(clicked()), this, SLOT(Addition()));
QObject::connect(ui.minusButton,SIGNAL(clicked()), this, SLOT(Subtraction()));
QObject::connect(ui.multiplyButton,SIGNAL(clicked()), this, SLOT(Multiplication()));
QObject::connect(ui.divisionButton,SIGNAL(clicked()), this, SLOT(Division()));
}
 
QtCalculator::~QtCalculator()
{
QString strFisrt = ui.lineEditNumber1->text();
QString strSecond = ui.lineEditNumber2->text();
 
bool ok;
int number1 = strFisrt.toInt(&ok);
int number2 = strSecond.toInt(&ok);
int result = number1 + number2;
 
QString strResult;
strResult = strResult.number(result);
ui.lineEditResult->setText(strResult);
}
 
void QtCalculator::Addition()
{
QString strFisrt = ui.lineEditNumber1->text();
QString strSecond = ui.lineEditNumber2->text();
 
bool ok;
int number1 = strFisrt.toInt(&ok);
int number2 = strSecond.toInt(&ok);
int result = number1 + number2;
 
QString strResult;
strResult = strResult.number(result);
ui.lineEditResult->setText(strResult);
}
 
void QtCalculator::Subtraction()
{
QString strFisrt = ui.lineEditNumber1->text();
QString strSecond = ui.lineEditNumber2->text();
 
bool ok;
int number1 = strFisrt.toInt(&ok);
int number2 = strSecond.toInt(&ok);
int result = number1 - number2;
 
QString strResult;
strResult = strResult.number(result);
ui.lineEditResult->setText(strResult);
}
 
void QtCalculator::Multiplication()
{
QString strFisrt = ui.lineEditNumber1->text();
QString strSecond = ui.lineEditNumber2->text();
 
bool ok;
int number1 = strFisrt.toInt(&ok);
int number2 = strSecond.toInt(&ok);
int result = number1 * number2;
 
QString strResult;
strResult = strResult.number(result);
ui.lineEditResult->setText(strResult);
}
 
void QtCalculator::Division()
{
QString strFisrt = ui.lineEditNumber1->text();
QString strSecond = ui.lineEditNumber2->text();
 
bool ok;
int number1 = strFisrt.toInt(&ok);
int number2 = strSecond.toInt(&ok);
int result = number1 / number2;
 
QString strResult;
strResult = strResult.number(result);
ui.lineEditResult->setText(strResult);
}


[edit] Conclusões

  • Será mostrada um simples calculadora na tela.

QtCalculatorScreenShot.png

[edit] Exemplo

[edit] Feedback

  • Se você econtrar algum problema na implementação desta simples calculadora, então por favor escreva seu feedback na página deste artigo Comment page (Inglês). Eu tentarei dá o máximo para tentar ajudar você.

[edit] Comentário

Código de erro resolvido.

This page was last modified on 11 October 2012, at 04:13.
167 page views in the last 30 days.
×