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

[SOLVED] Calling a function from another class

Scheduled Pinned Locked Moved General and Desktop
20 Posts 3 Posters 11.7k Views
  • 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.
  • P Offline
    P Offline
    p3c0
    Moderators
    wrote on 21 Jun 2014, 14:49 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 21 Jun 2014, 14:52 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
      • P Offline
        P Offline
        p3c0
        Moderators
        wrote on 21 Jun 2014, 15:06 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 21 Jun 2014, 15:10 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
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 21 Jun 2014, 15:16 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 21 Jun 2014, 15:20 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
              • P Offline
                P Offline
                p3c0
                Moderators
                wrote on 21 Jun 2014, 15:53 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 21 Jun 2014, 20:07 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 22 Jun 2014, 00:21 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 22 Jun 2014, 09:17 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 22 Jun 2014, 10:24 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 22 Jun 2014, 11:57 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

                          18/20

                          22 Jun 2014, 09:17

                          • Login

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