PDA

View Full Version : What is wrong with my code?



Dante
1st April 2009, 17:30
Calc++.h

#ifndef CALC++_H
#define CALC++_H

#include <QDialog>
#include "ui_Calc++.h"
class calc:public QDialog ,public Ui::Calc{
Q_OBJECT
private slots:
void edig(const char &num);
void set_op_op1(const char &op);
void compute_result();
public:
calc(QWidget *parent=0);
private:
double op1,op2;
char ope;};
#endif

Calc++.cpp

#include <QtGui>
#include "Calc++.h"
calc::calc(QWidget *parent):QDialog(parent){
lineEdit->setValidator(new QRegexpValidator(QDoubleValidator,this));
QObject::connect(cls,SIGNAL(clicked()),lineEdit,SL OT(clear()));
QObject::connect(_1,SIGNAL(clicked()),lineEdit,SLO T(edig(_1->text())));
QObject::connect(_2,SIGNAL(clicked()),lineEdit,SLO T(edig(_2->text())));
QObject::connect(_3,SIGNAL(clicked()),lineEdit,SLO T(edig(_3->text())));
QObject::connect(_4,SIGNAL(clicked()),lineEdit,SLO T(edig(_4->text())));
QObject::connect(_5,SIGNAL(clicked()),lineEdit,SLO T(edig(_5->text())));
QObject::connect(_6,SIGNAL(clicked()),lineEdit,SLO T(edig(_6->text())));
QObject::connect(_7,SIGNAL(clicked()),lineEdit,SLO T(edig(_7->text())));
QObject::connect(_8,SIGNAL(clicked()),lineEdit,SLO T(edig(_8->text())));
QObject::connect(_9,SIGNAL(clicked()),lineEdit,SLO T(edig(_9->text())));
QObject::connect(_10,SIGNAL(clicked()),lineEdit,SL OT(edig(_10->text())));
QObject::connect(point,SIGNAL(clicked()),lineEdit, SLOT(edig(point->text())));
QObject::connect(unmin,SIGNAL(clicked()),lineEdit, SLOT(edig('u')));
QObject::connect(plus,SIGNAL(clicked()),lineEdit,S LOT(set_op_op1('+')));
QObject::connect(minus,SIGNAL(clicked()),lineEdit, SLOT(set_op_op1('-')));
QObject::connect(multiply,SIGNAL(clicked()),lineEd it,SLOT(set_op_op1('*')));
QObject::connect(division,SIGNAL(clicked()),lineEd it,SLOT(set_op_op1('/')));
QObject::connect(equal,SIGNAL(clicked()),lineEdit, SLOT(compute_result()));}
void calc::edig(const char &num){
if(num=='u') lineEdit->setText(QString((char)((lineEdit->text().toDouble())*(-1))));
else lineEdit->setText(lineEdit->text()+num);}
void calc::set_op_op1(const char &op){
op1=(lineEdit->text()).toDouble();
ope=op;
lineEdit->clear();}
void calc::compute_result(){
op2=(lineEdit->text()).toDouble();
switch(ope){
case '+':
lineEdit->setText(QString((char)(op1+op2)));
break;
case '-':
lineEdit->setText(QString((char)(op1-op2)));
break;
case '*':
lineEdit->setText(QString((char)(op1*op2)));
break;
case '/':
lineEdit->setText(QString((char)(op1/op2)));
break;}}


ui_Calc++.h

/************************************************** ******************************
** Form generated from reading ui file 'Calc++.ui'
**
** Created: Wed Mar 4 15:07:20 2009
** by: Qt User Interface Compiler version 4.5.0
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
************************************************** ******************************/

#ifndef UI_CALC_2B__2B__H
#define UI_CALC_2B__2B__H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>

QT_BEGIN_NAMESPACE

class Ui_Calc
{
public:
QPushButton *point;
QPushButton *cls;
QPushButton *_3;
QPushButton *plus;
QPushButton *_7;
QPushButton *division;
QPushButton *_6;
QPushButton *_5;
QPushButton *_1;
QPushButton *unmin;
QPushButton *_4;
QPushButton *_8;
QPushButton *_9;
QPushButton *_2;
QPushButton *_10;
QLineEdit *lineEdit;
QPushButton *equal;
QPushButton *minus;
QPushButton *multiply;

void setupUi(QDialog *Calc)
{
if (Calc->objectName().isEmpty())
Calc->setObjectName(QString::fromUtf8("Calc"));
Calc->resize(245, 163);
point = new QPushButton(Calc);
point->setObjectName(QString::fromUtf8("point"));
point->setGeometry(QRect(100, 130, 31, 23));
cls = new QPushButton(Calc);
cls->setObjectName(QString::fromUtf8("cls"));
cls->setGeometry(QRect(200, 40, 31, 41));
_3 = new QPushButton(Calc);
_3->setObjectName(QString::fromUtf8("_3"));
_3->setGeometry(QRect(100, 40, 31, 23));
plus = new QPushButton(Calc);
plus->setObjectName(QString::fromUtf8("plus"));
plus->setGeometry(QRect(150, 40, 31, 23));
_7 = new QPushButton(Calc);
_7->setObjectName(QString::fromUtf8("_7"));
_7->setGeometry(QRect(20, 100, 31, 23));
division = new QPushButton(Calc);
division->setObjectName(QString::fromUtf8("division"));
division->setGeometry(QRect(150, 130, 31, 21));
_6 = new QPushButton(Calc);
_6->setObjectName(QString::fromUtf8("_6"));
_6->setGeometry(QRect(100, 70, 31, 23));
_5 = new QPushButton(Calc);
_5->setObjectName(QString::fromUtf8("_5"));
_5->setGeometry(QRect(60, 70, 31, 23));
_1 = new QPushButton(Calc);
_1->setObjectName(QString::fromUtf8("_1"));
_1->setGeometry(QRect(20, 40, 31, 23));
unmin = new QPushButton(Calc);
unmin->setObjectName(QString::fromUtf8("unmin"));
unmin->setGeometry(QRect(20, 130, 31, 23));
_4 = new QPushButton(Calc);
_4->setObjectName(QString::fromUtf8("_4"));
_4->setGeometry(QRect(20, 70, 31, 23));
_8 = new QPushButton(Calc);
_8->setObjectName(QString::fromUtf8("_8"));
_8->setGeometry(QRect(60, 100, 31, 23));
_9 = new QPushButton(Calc);
_9->setObjectName(QString::fromUtf8("_9"));
_9->setGeometry(QRect(100, 100, 31, 23));
_2 = new QPushButton(Calc);
_2->setObjectName(QString::fromUtf8("_2"));
_2->setGeometry(QRect(60, 40, 31, 23));
_10 = new QPushButton(Calc);
_10->setObjectName(QString::fromUtf8("_10"));
_10->setGeometry(QRect(60, 130, 31, 23));
lineEdit = new QLineEdit(Calc);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setGeometry(QRect(10, 10, 231, 21));
equal = new QPushButton(Calc);
equal->setObjectName(QString::fromUtf8("equal"));
equal->setGeometry(QRect(200, 90, 31, 61));
minus = new QPushButton(Calc);
minus->setObjectName(QString::fromUtf8("minus"));
minus->setGeometry(QRect(150, 70, 31, 23));
multiply = new QPushButton(Calc);
multiply->setObjectName(QString::fromUtf8("multiply"));
multiply->setGeometry(QRect(150, 100, 31, 23));

retranslateUi(Calc);

QMetaObject::connectSlotsByName(Calc);
} // setupUi

void retranslateUi(QDialog *Calc)
{
Calc->setWindowTitle(QApplication::translate("Calc", "Dialog", 0, QApplication::UnicodeUTF8));
point->setText(QApplication::translate("Calc", ".", 0, QApplication::UnicodeUTF8));
cls->setText(QApplication::translate("Calc", "&CLS", 0, QApplication::UnicodeUTF8));
_3->setText(QApplication::translate("Calc", "3", 0, QApplication::UnicodeUTF8));
plus->setText(QApplication::translate("Calc", "+", 0, QApplication::UnicodeUTF8));
_7->setText(QApplication::translate("Calc", "7", 0, QApplication::UnicodeUTF8));
division->setText(QApplication::translate("Calc", "/", 0, QApplication::UnicodeUTF8));
_6->setText(QApplication::translate("Calc", "6", 0, QApplication::UnicodeUTF8));
_5->setText(QApplication::translate("Calc", "5", 0, QApplication::UnicodeUTF8));
_1->setText(QApplication::translate("Calc", "1", 0, QApplication::UnicodeUTF8));
unmin->setText(QApplication::translate("Calc", "+/-", 0, QApplication::UnicodeUTF8));
_4->setText(QApplication::translate("Calc", "4", 0, QApplication::UnicodeUTF8));
_8->setText(QApplication::translate("Calc", "8", 0, QApplication::UnicodeUTF8));
_9->setText(QApplication::translate("Calc", "9", 0, QApplication::UnicodeUTF8));
_2->setText(QApplication::translate("Calc", "2", 0, QApplication::UnicodeUTF8));
_10->setText(QApplication::translate("Calc", "0", 0, QApplication::UnicodeUTF8));
lineEdit->setText(QString());
equal->setText(QApplication::translate("Calc", "=", 0, QApplication::UnicodeUTF8));
minus->setText(QApplication::translate("Calc", "-", 0, QApplication::UnicodeUTF8));
multiply->setText(QApplication::translate("Calc", "*", 0, QApplication::UnicodeUTF8));
Q_UNUSED(Calc);
} // retranslateUi

};

namespace Ui {
class Calc: public Ui_Calc {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_CALC_2B__2B__H


Hi
With due attention to codes above,I have 2 questions.
1-When I wanna run it,I get the Don't send message.What is the problem?
2-Whit due attention to line 4 of Calc++.cpp,Is it the right syntax for setting a validator for a lineEdit to QDoubleValidator?If not,tell the right one.thanks

spirit
1st April 2009, 17:36
very cool connection :D


QObject::connect(cls,SIGNAL(clicked()),lineEdit,SL OT(clear()));
QObject::connect(_1,SIGNAL(clicked()),lineEdit,SLO T(edig(_1->text())));
QObject::connect(_2,SIGNAL(clicked()),lineEdit,SLO T(edig(_2->text())));
QObject::connect(_3,SIGNAL(clicked()),lineEdit,SLO T(edig(_3->text())));
QObject::connect(_4,SIGNAL(clicked()),lineEdit,SLO T(edig(_4->text())));
QObject::connect(_5,SIGNAL(clicked()),lineEdit,SLO T(edig(_5->text())));
QObject::connect(_6,SIGNAL(clicked()),lineEdit,SLO T(edig(_6->text())));
QObject::connect(_7,SIGNAL(clicked()),lineEdit,SLO T(edig(_7->text())));
QObject::connect(_8,SIGNAL(clicked()),lineEdit,SLO T(edig(_8->text())));
QObject::connect(_9,SIGNAL(clicked()),lineEdit,SLO T(edig(_9->text())));
QObject::connect(_10,SIGNAL(clicked()),lineEdit,SL OT(edig(_10->text())));
QObject::connect(point,SIGNAL(clicked()),lineEdit, SLOT(edig(point->text())));
QObject::connect(unmin,SIGNAL(clicked()),lineEdit, SLOT(edig('u')));
QObject::connect(plus,SIGNAL(clicked()),lineEdit,S LOT(set_op_op1('+')));
QObject::connect(minus,SIGNAL(clicked()),lineEdit, SLOT(set_op_op1('-')));
QObject::connect(multiply,SIGNAL(clicked()),lineEd it,SLOT(set_op_op1('*')));
QObject::connect(division,SIGNAL(clicked()),lineEd it,SLOT(set_op_op1('/')));
QObject::connect(equal,SIGNAL(clicked()),lineEdit, SLOT(compute_result()));}

read this (http://www.qtcentre.org/forum/faq.php?faq=qt_signalslot)
PS. variables names "_<number>" pretty cool too. :D

pastor
1st April 2009, 17:42
1) You forgot setupUi(this)


calc::calc(QWidget *parent):QDialog(parent){
setupUi(this);
....
}

2) Please see Line Edits Example (QTDIR/examples/widgets/lineedits/)


PS: Hmmm, Is this 1st April joke? :D

spirit
1st April 2009, 17:57
2-Whit due attention to line 4 of Calc++.cpp,Is it the right syntax for setting a validator for a lineEdit to QDoubleValidator?If not,tell the right one.thanks

try this


...
lineEdit->setValidator(new QDoubleValidator(-999.0, 999.0, 2, lineEdit));
...


btw, is it some kind of joke? your code it is not runable at all. :)

Dante
2nd April 2009, 12:58
I got it running but the only button that works correctly is "CLS".
I think the connections have some problems as you said.
So could you tell me what is wrong with them?
And What is so funny?I'm just a beginner who wants some help.
thanks again

pastor
2nd April 2009, 13:06
Okay, sorry.


Can you zip your project and attach it here? I will try to help you.

spirit
2nd April 2009, 16:45
And What is so funny?I'm just a beginner who wants some help.

I apologize if I hurt you, but code really strange (looks like a joke). did you folow by this (http://www.qtcentre.org/forum/faq.php?faq=qt_signalslot#faq_qt_signalslot_with_n ames) link?
so, you have to rewrite you connections like this


...
connect(cls,SIGNAL(clicked()),lineEdit,SLOT(clear( )));
connect(_1,SIGNAL(clicked()), SLOT(setLineEditText()));
...
void calc::setLineEditText()
{
QPushButton *pb = qobject_cast<QPushButton *>(sender());
if (!pd)
return;

edig(pd->text());
}
...


setLineEditText it's a slot which you have to declare in you h-file.

Dante
2nd April 2009, 18:26
Hey Could you explain about the 7th line of your code!
And You mean connect has an overloaded version with 3 parameters?
And no you didn't hurt me.I just wanted to say "well every one starts somewhere".
And heres the link for downloading my project.
Calc++ (http://http://dante91.persiangig.com/Calc%2B%2B.zip)
Thanks again

spirit
2nd April 2009, 18:32
Hey Could you explain about the 7th line of your code!


read this (http://doc.trolltech.com/4.5/qobject.html#sender)



And You mean connect has an overloaded version with 3 parameters?


this is equivalent to this


connect(_1,SIGNAL(clicked()), this, SLOT(setLineEditText()));




And heres the link for downloading my project.
Calc++ (http://http://dante91.persiangig.com/Calc%2B%2B.zip)
Thanks again

I can't donwload an archive. error 404

EDITED: correct link http://dante91.persiangig.com/Calc%2B%2B.zip

spirit
3rd April 2009, 08:52
I've modified your example a little bit ;)

faldzip
3rd April 2009, 11:19
And I can't see any layouts... I suggest using layouts, as it can make you sure your app will look properly in any conditions :]

Dante
4th April 2009, 08:30
Hello again
I tried to add sth new to the calculator.and i was successful except about sqrt,sin,cos and tan because qmake gave error below about them:

[address] ~ can not be used as a function
I included <cmath>.pow,log,sinh,cosh and tanh work well.
And one more question.

lineEdit->setValidator(new QDoubleValidator(-1e100,1e100,10,lineEdit));
I think this command makes the lineEdit to accept every double to 10 floating point digits but it accepts more digits after floating point.and if you wanna show a floating point number,it shows just 5 digits after floating point and if you wanna show more tha 6 zeros it transforms to scientific notation.

Dante
5th April 2009, 16:53
Excuse me but can someone answer my question

wysota
5th April 2009, 18:10
What is the question?

Dante
5th April 2009, 20:48
Oh your right
well how can i use sin,cos,tan and sqrt
and how can i make the lineEdit to show numbers with more than 6 zeros(not user input)without using scientific notation and how can i make it to show floating pomt numbers with more than 5 digits after point(not user input) and one thing about user input the second quoted line in my two last post must make the lineEdit to accept just 10 integers after point but you can enter as many as you want.
How can i correct problems above?

spirit
5th April 2009, 21:05
Oh your right
well how can i use sin,cos,tan and sqrt
just include "#include <math.h>".

wysota
5th April 2009, 23:36
just include "#include <math.h>".

Or better yet "#include <cmath>" and use the std namespace.

About significant digits - remember floats have a limited precision. Use double instead of float wherever necessary. Scientific notation and "regular" notation has nothing to do with precision. If the precision is not what you wanted and you know the datatype can handle the required precision, it means you must have made a mistake while setting up the widget.

Dante
6th April 2009, 06:28
I included <cmath> and entered the line:

using namespace std
but it still gives the same error
and what you mean by making a mistake while setting up the widget?

wysota
6th April 2009, 06:37
Ok, but what is the error? :)

Dante
6th April 2009, 07:17
The one in i mentioned in my fourth post.


[address] ~ can not be used as a function

spirit
6th April 2009, 07:20
can you show us how you call this method? please, provide relevant code.

Dante
6th April 2009, 07:27
with having :

#include <QtGui>
#include <cmath>
#include "Calc++.h"

using namespace std;
at the top

else if (operation.contains("sqrt"))
if(m_op1<0){
lineEdit->setAlignment(Qt::AlignLeft);
lineEdit->setText("Sqrt accepts just positive numbers.");
}
else lineEdit->setText(QString::number(sqrt(m_op1)));
else if (operation.contains("sin"))
lineEdit->setText(QString::number(sin(m_op1)));
else if (operation.contains("cos"))
lineEdit->setText(QString::number(cos(m_op1)));
else if (operation.contains("tan"))
lineEdit->setText(QString::number(tan(m_op1)));

spirit
6th April 2009, 07:35
I don't see any problem. it should work fine.

Dante
6th April 2009, 07:36
So what should i do?

spirit
6th April 2009, 07:39
try to rebuild all project, make make clean, qmake, make.

Dante
6th April 2009, 07:43
No result!

spirit
6th April 2009, 07:47
ok, attach you sources again.

Dante
6th April 2009, 07:56
Hey we have a proverb that says

a shot in darkness
that means doing sth not very wise when you have no hope.well i did the same.
I added "std::" before those functions and i understood that there is no difference whether or not you write the line "using namespace std;" and you know,It worked.
thanks all of you