Call a class from a different source file

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

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


    What exactly I have to write?

    thanks in advance.

  • In def.cpp file there is one line:

    #include "abc.h"

    and the classtwo is a public slot.

    If I write:


    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.

  • what type is myaction and what type is *this?

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

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

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

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

  • Lifetime Qt Champion

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

    OtherClass * oc= new OtherClass(this);

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

    and check you have
    in the OtherClass

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

Log in to reply

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