[SOLVED] Calling a function from another class
-
Ok. So better would be to use the "Signals and Slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html mechanism.
Emit a custom signal say for eg. onSceneClicked() from mousePressEvent() function of nasaScena class.
And connect this signal to elipsaOFF() (you need to declare this as a slot) -
[quote author="p3c0" date="1403363202"]Ok. So better would be to use the "Signals and Slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html mechanism.
Emit a custom signal say for eg. onSceneClicked() from mousePressEvent() function of nasaScena class.
And connect this signal to elipsaOFF() (you need to declare this as a slot)[/quote]Interesting approach, will try. I thought something like calling a function would be the most basic way to perform it.
-
Signals and Slots is the most recommended and an important aspect of Qt programming.
-
[quote author="p3c0" date="1403363782"]Signals and Slots is the most recommended and an important aspect of Qt programming.[/quote]
Ok, I tried this (correct plz if I misunderstood)
@#include "nasascena.h"
#include "glavni.h"#include <QGraphicsSceneMouseEvent>
nasaScena::nasaScena()
{
connect(mousePressEvent, SIGNAL(event), glavni, SLOT(glavni::elipsaOFF()));
}void nasaScena::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//glavni g;
//g.elipsaOFF();
update();
QGraphicsScene::mousePressEvent(event);
}@This does not work; returns "expected primary-expression before ',' token...
points at "glavni," at the end of it in connect.
-
That won't work since event is not a signal and the Connect's syntax is wrong.
First you must declare a new signal in nasaScena.h
nasaScena.h
@
signals:
void onSceneClicked();
@Emit this signal in mousePressEvent
@
void nasaScena::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
emit onSceneClicked();
}
@Then in Main class glavni declare elipsaOFF() as a slot.
glavni.h
@
protected slots:
void elipsaOFF();
@and then in glavni.cpp connect the signal onSceneClicked() to the newly created slot. So
glavni.cpp
@
ui->setupUi(this);
scene= new nasaScena();
connect(scene,SIGNAL(onSceneClicked()),this,SLOT(elipsaOFF()));
@Thus the signal should trigger the slot.
-
[quote author="p3c0" date="1403365994"]That won't work since event is not a signal and the Connect's syntax is wrong.
First you must declare a new signal in nasaScena.h
nasaScena.h
@
signals:
void onSceneClicked();
@Emit this signal in mousePressEvent
@
void nasaScena::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
emit onSceneClicked();
}
@Then in Main class glavni declare elipsaOFF() as a slot.
glavni.h
@
protected slots:
void elipsaOFF();
@and then in glavni.cpp connect the signal onSceneClicked() to the newly created slot. So
glavni.cpp
@
ui->setupUi(this);
scene= new nasaScena();
connect(scene,SIGNAL(onSceneClicked()),this,SLOT(elipsaOFF()));
@Thus the signal should trigger the slot.[/quote]
Did all that, now it says: "undefined reference to 'nasaScena::onSceneClicked()'
this is on the @emit onSceneClicked();@ line. Googled for it, no luck. Why this won't work is beyond me. I declared the signal the way you said:in nasascena.h
@signals:
void onSceneClicked();@and it gives me shit when I try to emit it from nasascena.cpp
-
Hi Demmy,
What you seem to confused about is the following:
-
you have to call functions on instances of a class, in your case glavni
-
what you are doing is making a glavni instance g, and calling your function on that object
-
you need to which object you want to call your function on
-
best to get reading on C++ or object oriented programming in general
-
-
[quote author="t3685" date="1403428669"]Hi Demmy,
What you seem to confused about is the following:
-
you have to call functions on instances of a class, in your case glavni
-
what you are doing is making a glavni instance g, and calling your function on that object
-
you need to which object you want to call your function on
-
best to get reading on C++ or object oriented programming in general[/quote]
I thought it was something like that after p3c0 gave me an example with new glavni; but sadly I still don't understand how to call on existing object or how is it in this case even defined (can't call on "this" from other cpp file :D ). I guess I could use some serious reading.
-