[SOLVED] Calling a function from another class
-
-
Just use the new keyword.
So,
@
glavni *g = new glavni;
g->show();
g->elipsaOFF();
@ -
bq. change the existing one.
Do you mean you want to add this widget ?
-
[quote author="p3c0" date="1403361027"]bq. change the existing one.
Do you mean you want to add this widget ?[/quote]
Yes - add to the existing widget, glavni.cpp is a widget with a graphicsview and some other stuff in it and nasascena.cpp is basically qgraphicsscene which is imported in on the view in glavni.cpp. I want to be able to change things on the existing widget of glavni.cpp when I click inside the scene, changing the label name (using elipsaOFF()) was just a way for me to check on how it all works.
I have to figure it out somehow :)
For a future reference and why I'm asking for this:
I would like to get coordinates on a mouse click on the scene (already done below in the nasascena.cpp). They already are an extern float, but I would like to trigger a function inside glavni.cpp which would for example draw a line with coordinates provided.
I thought calling a function would be way simpler to do though. -
So basically you can use "setScene":http://qt-project.org/doc/qt-4.8/qgraphicsview.html#setScene function to set the QGraphicsScene to a QGraphicsView.
So in general for eg.
@
QGraphicsScene *m_scene = new QGraphicsScene(this);
ui->graphicsView->setScene(m_scene);
@Adjust the QGraphicView and QGraphicsScene Objects as per your need.
-
[quote author="p3c0" date="1403362183"]So basically you can use "setScene":http://qt-project.org/doc/qt-4.8/qgraphicsview.html#setScene function to set the QGraphicsScene to a QGraphicsView.
So in general for eg.
@
QGraphicsScene *m_scene = new QGraphicsScene(this);
ui->graphicsView->setScene(m_scene);
@Adjust the QGraphicView and QGraphicsScene Objects as per your need.[/quote]
it is already done:
glavni.cpp
@
#include "glavni.h"
#include "ui_glavni.h"
#include "nasascena.h"
#include "nasaelipsa.h"#include<QGraphicsScene>
glavni::glavni(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::glavni)
{
ui->setupUi(this);scene= new nasaScena(); ui->graphicsView->setScene(scene); elipsa[0] = new nasaElipsa(); //irrelevant in this case scene->addItem(elipsa[0]); //irrelevant in this case
}
glavni::~glavni()
{
delete ui;
}void glavni::elipsaOFF()
{
//Pritisnuto=true;
//elipsaToggle();
ui->label->setText("YOU PRESSED IT");
//elipsaON();
}
@
at this point it is very important to me to somehow trigger this function though, scene and view work fine.Pic:
http://i58.tinypic.com/33e6duh.jpg
but it is all irrelevant at this point, all I want to do is to execute elipsaOFF() in glavni.cpp from nasascena.cpp without creating a completely new window.
-
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.
-