View Full Version : How to call a function from another cpp file
herculis
22nd September 2014, 17:34
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.
anda_skoa
22nd September 2014, 18:27
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,
_
herculis
23rd September 2014, 09:44
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
aamer4yu
23rd September 2014, 10:19
Can you show some code. Hard to know where you are going wrong
herculis
23rd September 2014, 10:38
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
west
23rd September 2014, 11:08
1. Inherit QObject.
2. Use Q_OBJECT macro.
3. Declare this function as a slot.
herculis
23rd September 2014, 11:26
I have written Q_OBJECT in Header.h
and sorry i didnt get what you have written plz can u explain in detail
anda_skoa
23rd September 2014, 12:15
http://qt-project.org/doc/qt-5/signalsandslots.html
Cheers,
_
herculis
25th September 2014, 13:52
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.
anda_skoa
25th September 2014, 16:08
There is an example in the link I've posted.
Additionally most examples that come with Qt are doing that.
Cheers,
_
wysota
25th September 2014, 17:09
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:
#include <cstdio>
void funToCall() { printf("Someone called?\n"); }
file1.h:
void funToCall();
file2.cpp:
#include "file1.h"
void main() {
funToCall();
}
However I'm sure the question you asked was not really the one you needed to have an answer to.
herculis
26th September 2014, 09:05
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)
anda_skoa
26th September 2014, 09:58
Why would that be a problem?
Just pass the required values, like for any other function.
Cheers,
_
wysota
26th September 2014, 10:31
ap(bool*a ,QWidget *parent = 0,QObject *parent)
I'm sure you don't have such constructor as this code wouldn't compile.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.