PDA

View Full Version : A little calculator problem



bijan311
8th March 2010, 21:21
OK so I started to make a calculator program and was compiling while I was coding (that's why the code isn't complete), and it gave me these errors.


error: invalid conversion from `int' to `const QObject*'
error: initializing argument 3 of `static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)'


Here is my code.


#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QLCDNumber>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *minus = new QLabel("-");
QPushButton *equal = new QPushButton("Execute");
QSpinBox *num1 = new QSpinBox;
QSpinBox *num2 = new QSpinBox;
QLCDNumber *show = new QLCDNumber(3);
int calc1, calc2;

QObject::connect(num1, SIGNAL(valueChanged(int)), calc1, SLOT(setValue(int)));

return app.exec();
}

dubstar_04
8th March 2010, 21:38
try:

QObject::connect(num1, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));

toutarrive
8th March 2010, 21:49
You are passing an int (calc1) as third argument while the function is waiting for a QObject.

The error message gives you the proper signature of QObject::connect .
The parameters must be the as follow :
(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType.

bijan311
8th March 2010, 22:07
@Dubstar_04 if i do that how will I get calc 1 to equal the value of the spinbox?

toutarrive
9th March 2010, 07:10
Set the calc1 value in your slot setValue(int).

Lykurg
9th March 2010, 07:41
Maybe you should tell us first what you want achieve. Because your code is nonsense! And I can't figure out why you what set an integer whenever the value of a spinbox is changed, since you can access that info via QSpinBox::value().

bijan311
9th March 2010, 13:45
yah, I know my code is nonesense, that's because it isn't finished. I'm trying to make a program that subtracts the two numbers in the spin-boxes when you press the number.

bijan311
9th March 2010, 13:50
OK, so I did this,


QObject::connect(num1, SIGNAL(valueChanged(calc1)), this, SLOT(setValue(calc1)));
QObject::connect(num2, SIGNAL(valueChanged(calc2)), this, SLOT(setValue(calc2)));

but now it says this,


error: invalid use of `this' in non-member function

Thank you.

wysota
9th March 2010, 13:51
Shouldn't you be connecting to the clicked() signal of the "Execute" button then?

bijan311
9th March 2010, 14:00
OK, here is the entire code,


#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QLCDNumber>
#include <QHBoxLayout>
#include <QVBoxLayout>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *minus = new QLabel("-");
QPushButton *equal = new QPushButton("Execute");
QSpinBox *num1 = new QSpinBox;
QSpinBox *num2 = new QSpinBox;
QLCDNumber *show = new QLCDNumber(3);
int calc1, calc2;

QObject::connect(num1, SIGNAL(valueChanged(calc1)), this, SLOT(setValue(calc1)));
QObject::connect(num2, SIGNAL(valueChanged(calc2)), this, SLOT(setValue(calc2)));
QObject::connect(equal, SIGNAL(clicked()), show, SLOT(setValue(calc1-calc2)));

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(num1);
layout->addWidget(minus);
layout->addWidget(num2);
layout->addWidget(show);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(layout);
mainLayout->addWidget(equal);

return app.exec();
}

and here are the errors I receive.


|19|error: invalid use of `this' in non-member function|
|20|error: invalid use of `this' in non-member function|

wysota
9th March 2010, 14:07
Well, there is no "this" object when you are not inside any objective code so obviously it won't work. Your connect statements still don't make sense anyway. You can't do mathematical operations there and you can't pass variable names there.

Here is something more reasonable:

class Calculator : public QWidget {
Q_OBJECT
public:
Calculator(QWidget *parent = 0) : QWidget(parent){
minus = new QLabel("-");
equal = new QPushButton("Execute");
num1 = new QSpinBox;
num2 = new QSpinBox;
show = new QLCDNumber(3);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(num1);
layout->addWidget(minus);
layout->addWidget(num2);
layout->addWidget(show);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(layout);
mainLayout->addWidget(equal);
connect(equal, SIGNAL(clicked()), show, SLOT(calculate()));
}
public slots:
void calculate() {
show->setValue(num1->value()-num2->value());
}
private:
QLabel *minus;
QPushButton *equal;
QSpinBox *num1;
QSpinBox *num2;
QLCDNumber *show;
};

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
Calculator w;
w.show();
return app.exec();
}

bijan311
9th March 2010, 22:32
now it says


|error: 'class QLCDNumber' has no member named 'setValue'



P.S
Sorry this is taking this long, I am a complete beginner.

Lykurg
9th March 2010, 22:43
It's because it has to be
show->display(num1->value()-num2->value());. A quick look it the documentation helps in such cases.

bijan311
9th March 2010, 22:54
sorry, I didn't post these errors and I thought the others would go away but here are all the errors.


|40|error: `QLCDNumber*calculator::show' is private|
|48|error: within this context|
|48|error: `w.calculator::show' cannot be used as a function|


again, sorry if I'm beginning to be a pain.

wysota
10th March 2010, 00:02
Call the variable differently. It clashes with the method name (QWidget::show()).

Lykurg
10th March 2010, 00:08
Seems to be a name clash, so rename your QLCDNumber to lcdnumber or something else and recompile.

bijan311
10th March 2010, 01:34
OK so everything compiled, BUT when it is linking to the .EXE it gives me this error


C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\c++\3.4.5\bits\stl_algobase. h:(.text$_ZN10calculatorD1Ev[calculator::~calculator()]+0xb)||undefined reference to `vtable for calculator'|

wysota
10th March 2010, 08:51
Did you remember to run qmake after making changes I initially suggested to the code?

bijan311
10th March 2010, 13:35
I'm sorry, I don't know how to make a .moc file, I was playing around in the QT command prompt but I can't get a .moc file.

I've treid qmake, qmake -o file, qmake -v.

squidge
10th March 2010, 13:43
Run QMake and then rebuild. That'll create your moc files and even run moc on them

bijan311
10th March 2010, 13:57
ok i ran qmake in the folder of main.cpp then I pressed rebuild in Code::Blocks and it gave me the same error.

wysota
10th March 2010, 14:32
Did you implement the class destructor?

squidge
10th March 2010, 17:31
ok i ran qmake in the folder of main.cpp then I pressed rebuild in Code::Blocks and it gave me the same error.

Dump Code::Blocks, download Qt Creator. It will make your life easier. Once you have learned the basics, you can go back to Code::Blocks if you wish.

(For example, in Qt Creator there is no need for a console as you can do everything from inside the IDE)

bijan311
10th March 2010, 22:24
ok so I have installed QT Creator.

and what is a class destructor.

wysota
10th March 2010, 23:49
and what is a class destructor.

Ouch... we didn't learn C++, did we?

http://en.wikipedia.org/wiki/Destructor_(computer_science)
http://www.parashift.com/c++-faq-lite/dtors.html

bijan311
11th March 2010, 00:11
I did learn c++ from
cprogramming.com (http://www.cprogramming.com)
I just never had a good understanding of classes.

wysota
11th March 2010, 00:51
So you were learning C++ but didn't learn it. It's all about classes. It's like you were learning a language but never had a good understanding of words... I suggest you correct that mistake now.

bijan311
11th March 2010, 02:06
well actually, I got a better understanding of classes when reading this thread...

bijan311
11th March 2010, 02:59
OK, I got it to compile but when i press execute the LCDNumber thing doesn't change.
here is the cofr


#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QLCDNumber>
#include <QHBoxLayout>
#include <QVBoxLayout>

class calculator : public QWidget{

public:
calculator(QWidget *parent = 0) : QWidget(parent)
{
equal = new QPushButton("Execute");
num1 = new QSpinBox;
num2 = new QSpinBox;
answer = new QLCDNumber;

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(num1);
layout->addWidget(num2);
layout->addWidget(answer);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(layout);
mainLayout->addWidget(equal);

QObject::connect(equal, SIGNAL(clicked()), answer, SLOT(minus()));
}
public slots:
void minus(){
answer->display(num1->value()-num2->value());
}
private:
QPushButton *equal;
QSpinBox *num1;
QSpinBox *num2;
QLCDNumber *answer;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
calculator w;

w.show();
return app.exec();
}

wysota
11th March 2010, 07:08
OK, I got it to compile but when i press execute the LCDNumber thing doesn't change.
This doesn't surprise me as your code is not correct. Please open Qt Reference and read what arguments should you pass to connect(). Currently you miss some basic understanding of the signal/slot mechanism. Besides, you have already been given a working solution.

bijan311
11th March 2010, 23:13
I only made a few changes. I got rid of the Q_Object macro because with it the program wouldn't link to the .exe file. I tried to make a .moc file by using qmake in the directory of the program, the only files that are there are:

Calculator Gui.depend
Calculator Gui.layout
Calculator Gui.pro
Calculator Gui.pro.user
file
file.Debug
file.Release
Makefile
Makefile.Debug
Makefile.Release

thank you.

squidge
12th March 2010, 08:07
Watch the output window in Qt Creator, it'll tell you what your doing wrong. I can see an instant problem with your connect() call.

wysota
12th March 2010, 08:28
I only made a few changes. I got rid of the Q_Object macro because with it the program wouldn't link to the .exe file.

This macro wasn't there because it looks nice, placing it there had its purpose. I really really suggest you read a bit of docs before you start programming with Qt. At least go through the tutorial.

sutee84
12th March 2010, 09:35
QtCreator maybe can help you.

wysota
12th March 2010, 09:50
QtCreator maybe can help you.

No, Qt Creator won't help him understand what he is doing.

sutee84
14th March 2010, 07:45
You're right, but it's easier to connect signals and slots in QtCreator on the right way.

squidge
14th March 2010, 17:17
You're right, but it's easier to connect signals and slots in QtCreator on the right way.Yes, if you know where the signals and slots need to be connected, but if you don't know that it could just become even more confusing when it doesn't let you do what you think is correct (like connecting a signal to a integer variable).

Reading the documentation or tutorial is a must before attempting anything, even a simple calculator program. If your a seasoned developer, the tutorial will probably take you about 10 minutes to go through.

wysota
15th March 2010, 01:46
You're right, but it's easier to connect signals and slots in QtCreator on the right way.

But you have to choose the right object as the source and destination of the signal. Without that Creator will not be helpful in any way other than not listing the signals and slot you'd like to connect which suggests you did something wrong. But then the compiler/Qt pair do exactly the same thing.