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 thisP.S. i have added header file of second file(where the function is) also in the first file.
-
Hi,
Please, take the time to look at Qt's documentation as well as search this forum, this question has been asked many times.
-
i didnt find any post related to this question
Please help -
Please, show some problem code.
Your function is class member? -
Header.h
@
class ap
{
Q_OBJECT
public slot:
void open();
}call.cpp
include header.h
{
class ap:: open()
{
}
}
@on.cpp
@
include header.h
{
connect(openButton,SIGNAL(clicked), this, SLOT(open));
}
@[edit: added missing coding tags @ SGaist]
-
its like signal(clicked),this,slot(open)
-
Please, use tags for code.
Maybe you need something like?
@
// call.h
class call : public QObject
{
Q_OBJECTexplicit call(QObject *parent)
public slot:
void open();
}// call.cpp
#include "call.h"
#include <qdebug.h>call::call(QObject *parent): QObject(parent)
{}call::open()
{
qDebug() << "CALL";
}// on.h
#include "call.h"
class on : public QObject
{
Q_OBJECT
explicit on(QObject *parent)signals:
void mySignal();private:
call *m_call;
}// on.cpp
on::on(QObject *parent): QObject(parent)
{
m_call = new call(this);connect(this, &on::mySignal, m_call, &call::open);
emit mySignal();
}
@ -
I am geeting error at line 41
@m_call = new call(this);@
no instance of constructor
no appropriate default constructor available -
Sorry!) I updated my answer
-
what u updated ???
-
See my above answer:
@
// call.h
class call : public QObject
{
Q_OBJECTexplicit call(QObject *parent)
public slot:
void open();
}
...@ -
i did what u suggested but its not working.Not getting any error but not working also.
plz help -
@
Header.h
class ap
{
Q_OBJECT
public slot:
void open();
}call.cpp include header.h { class ap:: open() { } }
on.h
include header.h
{
class on
private:
ap*m_call}
on.cpp
include header.h {
m_call = new ap(this);
connect(openButton,SIGNAL(clicked), m_call, SLOT(open));@ -
Try rename file names: you must have 4 files:
call.h
call.cpp
on.h
on.cppAnd, please show to us all files acrtual contents, not pseudocode.
-
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 command but its not working please help
can a one give me an exmaple or a link for "calling a function from other cpp file" so that i will get an idea how to do that. -
Just create new project (Test) and add there files:
@
// call.h
#ifndef CALL_H
#define CALL_H#include <QObject>
class call : public QObject
{
Q_OBJECTpublic:
explicit call(QObject *parent = 0);public slots:
void open();
};#endif // CALL_H@
@
// call.cpp
#include "call.h"
#include <qdebug.h>call::call(QObject *parent): QObject(parent)
{}void call::open()
{
qDebug() << "CALL";
}
@@
// on.h
#ifndef ON_H
#define ON_H#include <QObject>
#include "call.h"class on : public QObject
{
Q_OBJECTpublic:
explicit on(QObject *parent = 0);signals:
void mySignal();private:
call *m_call;
};#endif // ON_H
@@
// on.cpp
#include "on.h"on::on(QObject *parent): QObject(parent)
{
m_call = new call(this);connect(this, &on::mySignal, m_call, &call::open);
emit mySignal();
}
@@
// main.cpp
#include "on.h"int main(int argc, char *argv[])
{
on *myOn = new on();
}
@@
// Test.pro
HEADERS +=
call.h
on.hSOURCES +=
call.cpp
on.cpp
main.cpp
@ -
Thanks this works but in my code its not working dont know why
Thanks though. -
i am getting error in my prog when i write
@m_call=new call (this)@
''that no instance of constructor'' -
Simpify the code and show it.
-
ok now i know the problem when i define the class contructore 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
@call(boola ,QWidget *parent = 0,QObject *parent)@