Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Calling a function from another class
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Calling a function from another class

Scheduled Pinned Locked Moved General and Desktop
20 Posts 3 Posters 12.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Demmy
    wrote on last edited by
    #8

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

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #9

      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.

      157

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Demmy
        wrote on last edited by
        #10

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

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #11

          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)

          157

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Demmy
            wrote on last edited by
            #12

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

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #13

              Signals and Slots is the most recommended and an important aspect of Qt programming.

              157

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Demmy
                wrote on last edited by
                #14

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

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #15

                  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.

                  157

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Demmy
                    wrote on last edited by
                    #16

                    [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

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Demmy
                      wrote on last edited by
                      #17

                      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.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        t3685
                        wrote on last edited by
                        #18

                        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

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Demmy
                          wrote on last edited by
                          #19

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

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            t3685
                            wrote on last edited by
                            #20

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

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved