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. Call a class from a different source file
Forum Updated to NodeBB v4.3 + New Features

Call a class from a different source file

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.3k 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.
  • KonstantinosK Offline
    KonstantinosK Offline
    Konstantinos
    wrote on last edited by Konstantinos
    #1

    void classone::classtwo(void) in the abc.cpp file

    in def.cpp file, I want to connect the myaction with the classtwo.

    connect(myaction,SIGNAL(triggered())...

    What exactly I have to write?

    thanks in advance.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      http://www.learncpp.com/cpp-tutorial/19-header-files/

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • KonstantinosK Offline
        KonstantinosK Offline
        Konstantinos
        wrote on last edited by VRonin
        #3

        In def.cpp file there is one line:

        #include "abc.h"

        and the classtwo is a public slot.

        If I write:

        connect(myaction,SIGNAL(triggered()),this,SLOT(classtwo(void)));
        

        It says: Object::connect: No such slot ...::classtwo(void) in ..def.cpp

        I want it to search not in def.cpp but in abc.cpp

        This is what I am asking. What I have to change.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          what type is myaction and what type is *this?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • KonstantinosK Offline
            KonstantinosK Offline
            Konstantinos
            wrote on last edited by VRonin
            #5

            QAction* myaction

            But I think I have to find a way to call a function in another source file. Something I have to put instead of this.

            For example

            connect(myaction,SIGNAL(triggered()),void classone::classtwo(void),SLOT(classtwo(void)));
            

            it does not work.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              you need a pointer to an instance of classone. can you post the full content of abc.h, def.cpp and wherever you are calling connect from?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • KonstantinosK Offline
                KonstantinosK Offline
                Konstantinos
                wrote on last edited by
                #7

                It is difficult to post all the code. But can you tell me more details about a pointer to an instance of classone? I think I have to do something like this.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  http://doc.qt.io/qt-4.8/signalsandslots.html#a-small-example

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • KonstantinosK Offline
                    KonstantinosK Offline
                    Konstantinos
                    wrote on last edited by
                    #9

                    Thanks. This is what I am talking about. In abc.h:

                    public slots:

                    void classtwo(void)

                    How can I call this public slot, not from abc.cpp, but from another .cpp file, for example from def.cpp?

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      To connect, you need two live objects.
                      So you need to create one. (at least )
                      somewhere in def.cpp

                      OtherClass * oc= new OtherClass(this);

                      then
                      connect(this (the def class), (def)signal, oc , theslot (in OtherClass ) )

                      and check you have
                      Q_OBJECT
                      in the OtherClass

                      1 Reply Last reply
                      2
                      • KonstantinosK Offline
                        KonstantinosK Offline
                        Konstantinos
                        wrote on last edited by
                        #11

                        Thank you mrjj. This was exactly what I did want.

                        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