Alan os Alan os - 4 months ago 27
C++ Question

QT call function from another .cpp file c++

I'm currently working on my first GUI cpp application using QT. Right now my aim is to display a dialogue to the user with two buttons: Ok and Cancel. I've used signal and slot editor to make cancel close the window, however what I can't seem to find and manage to do is to run a function from another .cpp file when OK is being clicked. I've googled it and no answers seem to be relevant.

file "mydefault.cpp" -

void MyDefault::on_pushButton_clicked()
\\ call function from another .cpp file

function which im trying to call in "server.cpp"

void calldefaults(){

Question - how to call function "calldefaults()" in "server.cpp" from "mydefaults.cpp"

Any clues or tips are more than welcome, if the answer is super dump just throw it at me :)



This is important thing to understand in C / C++ (as opposed to Java, etc). Each .c/.cpp file is compiled separately, then linked together. When the mydefault.cpp is being compiled, it needs to know that the calldefaults() function exists. To do that, you need what's called a "prototype". At the beginning of the mydefault.cpp file, outside of any function declaration, put a line that looks like:

void calldefaults ();

and call the function like:


That will work - if you have linking issues, ensure both files are included in your compiler command.