PDA

View Full Version : Can we call functions of other process in Qt GUI application?



Krish
3rd March 2008, 18:11
Hello! Friends,
I have a square .cpp program for squaring the given number but it is another pure .cpp file called "square.cpp" file containing no Qt classes or headers but which i have placed in the same GUI project with other files. Now what i am planning is to call the "square_of(int i)" function from square.cpp file in my GUI .cpp file, just by including the square.h file in GUI .cpp file and get the result of squaring in my GUI. Is it possible????

If yes how? and if not any other solution?

I will be obliged if replied.-----> Thanks in Advance.

jpn
3rd March 2008, 18:27
How about reading http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html ? :)

Krish
4th March 2008, 08:21
Hello! jpn,
Thanks for replying and showing me that site. But Sir i know C++ programming, what i dont know is can we call that function from GUI .cpp file or not. Sir it is serious!!!!

Below are the files: -


/////////-------Square.h---------///////////
#ifndef SQUARE_H
#define SQUARE_H

#include<iostream>

class Square
{
public:
Square();
int Square_of(int);
}
#endif
////////----End of file-------/////////




///////-----Suare.cpp------//////////
#include<square.h>

int Square::Square_of( i )
{
return i*i;
}
/////////-------End of file------/////////




////////-------testform.ui.h 'which is GUI form's file'------//////////
#include<square.h>

void testForm::square()
{
int i = inputLineEdit->text().toInt();
int sq = Square_of(i);
resultLineEdit->setText(QString::number(sq,10));
}

void testForm::init()
{
resultLineEdit->setText("2");
resultLineEdit->selectAll();
}
///////------End of file-------///////////




/////////-------main.cpp 'which is GUI's execution file---------/////////
#include <qapplication.h>
#include "test1form.h"

int main( int argc, char ** argv )
{
QApplication a( argc, argv );
test1Form w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
////////------End of file-----/////////


When i tried to execute it like by using qmake & make it showed me some errors: -


square.cpp:5: error: extraneous `int' ignored
square.cpp:5: error: `Square Square::Square_of' is not a static member of `class Square'
square.cpp:5: error: `i' was not declared in this scope
square.cpp:6: error: expected `,' or `;' before '{' token
make: *** [.obj/square.o] Error 1


Can you please let me know what is causing these error's.

I will be obliged if helped.-------->Thanks in Advance.

jpn
4th March 2008, 08:35
First of all, this is a very basic C++ problem and has nothing to do with Qt. Secondly, this is still a very basic C++ problem and I really really recommend reading a C++ book.

It's an unfortunate fact that you must know the basics of the programming language before you start working with a toolkit like Qt. Some language bindings might be easier to start with but especially C++ requires some knowledge. The problem here is that unless you make the method static, you must instantiate a Square object to be able to call its member function. Oh, and the syntax is still not correct even if it was static...