Results 1 to 14 of 14

Thread: How to call a function from another cpp file

  1. #1
    Join Date
    Jul 2014
    Posts
    39
    Qt products
    Qt5
    Platforms
    Windows

    Default How to call a function from another cpp file

    hello guys
    i want to call a function in one cpp file from another cpp file.
    and i have written this function to slot
    Can anyone please help me in this

    P.S. i have added header file of second file(where the function is) also in the first file.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a function from another cpp file

    C++ doesn't make any distinction on where a function is defined.
    If it is declared, as it should be for you since you included the header that declares it, just call it.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2014
    Posts
    39
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to call a function from another cpp file

    i am calling it in slot but its not working.
    means this function is attached to pushbutton, when i click pushbutton its not calling the function.
    can you help in this.
    Thanks

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call a function from another cpp file

    Can you show some code. Hard to know where you are going wrong

  5. #5
    Join Date
    Jul 2014
    Posts
    39
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to call a function from another cpp file

    i cant show the code but abstact is as follows
    Header.h
    {
    class ap
    void open();
    }


    call.cpp
    include header.h
    {
    class ap:: open()
    {
    }
    }


    on.cpp
    include header.h
    {
    connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
    }

    I think i am not sure
    problem is i am using 'this' in connect command that why its not working what shuld i write instead of that

  6. #6
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to call a function from another cpp file

    1. Inherit QObject.
    2. Use Q_OBJECT macro.
    3. Declare this function as a slot.

  7. #7
    Join Date
    Jul 2014
    Posts
    39
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to call a function from another cpp file

    I have written Q_OBJECT in Header.h
    and sorry i didnt get what you have written plz can u explain in detail

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a function from another cpp file


  9. #9
    Join Date
    Jul 2014
    Posts
    39
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to call a function from another cpp file

    i added header file in the program but then also its not working i have to create instance, with this it will work i guess i did that also means i created one object of the class where the function is and put that object in connect commad but its not working please help
    can a one give me an exmaple or a link for callin a function from other cpp file so that i will get an idea how to do that.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a function from another cpp file

    There is an example in the link I've posted.
    Additionally most examples that come with Qt are doing that.

    Cheers,
    _

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to call a function from another cpp file

    Quote Originally Posted by herculis View Post
    can a one give me an exmaple or a link for callin a function from other cpp file so that i will get an idea how to do that.
    Here is an example of calling "funToCall()" defined in file1.cpp from within file2.cpp:

    file1.cpp:
    Qt Code:
    1. #include <cstdio>
    2.  
    3. void funToCall() { printf("Someone called?\n"); }
    To copy to clipboard, switch view to plain text mode 

    file1.h:
    Qt Code:
    1. void funToCall();
    To copy to clipboard, switch view to plain text mode 

    file2.cpp:
    Qt Code:
    1. #include "file1.h"
    2.  
    3. void main() {
    4. funToCall();
    5. }
    To copy to clipboard, switch view to plain text mode 

    However I'm sure the question you asked was not really the one you needed to have an answer to.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jul 2014
    Posts
    39
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to call a function from another cpp file

    actually the problem is when i define the class contructor it has arguments in it thats why i am not able to create object.Do you know any solution regrading this how can i create object of a class whose constructor has arguments like bool*a,QWidget *parent = 0
    means

    ap(bool*a ,QWidget *parent = 0,QObject *parent)

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a function from another cpp file

    Why would that be a problem?

    Just pass the required values, like for any other function.

    Cheers,
    _

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to call a function from another cpp file

    Quote Originally Posted by herculis View Post
    ap(bool*a ,QWidget *parent = 0,QObject *parent)
    I'm sure you don't have such constructor as this code wouldn't compile.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 2nd December 2013, 04:43
  2. Replies: 3
    Last Post: 17th October 2013, 08:12
  3. Replies: 4
    Last Post: 2nd August 2012, 07:42
  4. How to call cpp file function from qml??
    By naufalahad in forum Qt Quick
    Replies: 5
    Last Post: 30th January 2012, 09:52
  5. Qt function call in vb.net
    By abghosh in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2010, 17:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.