[SOLVED] Calling a function from another class



  • I understand this is an extremely beginner's question and more related to c++ itself than it is to qt but sadly I cannot seem to understand where the problem is at this point.

    Intention: I wish to call a function which is located in glavni.cpp and set under public in glavni.h, from nasascena.cpp when a mouse event is triggered.

    Current situation: I got the mousepressevent itself to work, so the only thing left is calling a function.

    glavni.cpp
    @
    //bunch of stuff
    void glavni::elipsaOFF()
    {
    ui->label->setText("YOU PRESSED IT");
    }
    @

    glavni.h
    @class glavni : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit glavni(QWidget *parent = 0);
    ~glavni();

    void elipsaOFF();
    

    //other stuff@

    nasascena.cpp
    @#include "nasascena.h"
    #include "glavni.h"

    #include <QGraphicsSceneMouseEvent>
    nasaScena::nasaScena()
    {
    }

    void nasaScena::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    glavni g;
    g.elipsaOFF();
    //other working stuff @

    What happens: It runs, no errors. When I get to the mouse event, everything under it executes except the g.elipsaOFF(); part
    I tried going glavni::elipsaOFF(); with the static void, but it didn't work as intended. Any tips?



  • What do you mean nothing happens? I suspect the text of your label isn't changing?


  • Moderators

    Hi,

    The glavni class object is destroyed as soon as the function exits and hence nothing is displayed.
    You should rather create the object on heap.



  • @t3685

    Yes, the label does not change. I previously put in some other functions from the same class to call and they do not execute. If I put in a variable, it does not change. It is as if the function does not trigger at all.

    @p3c0

    Can you suggest how it should look? Would be much appriciated.


  • Moderators

    Just use the new keyword.
    So,
    @
    glavni *g = new glavni;
    g->show();
    g->elipsaOFF();
    @



  • [quote author="p3c0" date="1403359381"]Just use the new keyword.
    So,
    @
    glavni *g = new glavni;
    g->show();
    g->elipsaOFF();
    @[/quote]

    ok, this works but it creates a completely new window. I would like to change the existing one.


  • Moderators

    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.


  • Moderators

    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.


  • Moderators

    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.


  • Moderators

    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.


  • Moderators

    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



  • Ok, apparently I had to add Q_OBJECT macro in nasascena.h and run qmake for it to work. Although I'd still like to know how to run a function from the outside I declare this problem solved. Many thanks to p3c0 for bearing with me.



  • 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.



  • Yes, reading up a bit would do you good :). It's not difficult but you need to know some basic things.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.