PDA

View Full Version : Dynamically add functions to qt program



sincnarf
30th August 2007, 14:31
Anybody here can point me to examples of how a user can create his/her own pure C++ function then the newly created function can be dynamically added to a list of functions available for use within the qt program. The list of functions are available in a menu. The user can also delete functions from (and add functions to) the menu.

marcel
30th August 2007, 14:36
You mean something like some sort of application plugins?

Wizard
30th August 2007, 20:33
Or you mean something like dynamic C++ ? It's very difficult to implement this idea. It's better to try QtScript if you wish some kind of self-modifying application...

sincnarf
1st September 2007, 03:22
This is indeed a hard task. In any case, my ears are open to all your ideas. Someone in another forum site told me to do the following:



1. Define your function/method.

2. Use the QMenu::addAction(QString) method to add a new action to a menu. It returns a pointer to the action.

3. Use the connect() method to connect the triggerer() Signal of the new QAction object to the function/method that you defined.

Now every time you click on the menu item that was added your function/method will be executed.


The main problem though is No. 1
1. Define your function/method., I can't Imagine how I can do this. My objective is that for example my application has a "Create Function" button, on clicking, some kind of dialog will appear and that's where the user can create his or her own function. But he/she can only do this if he/she creates his/her own library! How can that user do that?


For example, the user created binary.h and binary.cpp (assuming imports/includes for these are already part of the application). How can these libraries be integrated in the program while the program is already running?

1518

1519

marcel
1st September 2007, 08:33
I told you.
You need an application plugin( you know, something like eclipse or designer plugins).
Read http://doc.trolltech.com/4.3/plugins-howto.html and additionally you can search this forum.

Regards

Kumosan
1st September 2007, 10:17
For example, the user created binary.h and binary.cpp (assuming imports/includes for these are already part of the application). How can these libraries be integrated in the program while the program is already running?


I would say: Forget it. Your user would have to write C++ within you application, compile it with an external compiler, link it during runtime to the application he is currently in?

What on earth are you doing? What user base do you have? Did you think of the security implications of such a model?

Nevertheless, I suppose you thought of all this. It can be done using QProcess to compile your stuff and using plugins to add the freshly compiled lib to your running application.

For an example of plugins:

http://spectrascan.svn.sourceforge.net/viewvc/spectrascan/SpectraScan/MainWidget/ExportPluginList.cpp?revision=52&view=markup
Method 'ExportPluginList::loadPlugins()'
and
http://spectrascan.svn.sourceforge.net/viewvc/spectrascan/SpectraScan/MainWidget/MenuBars/Actions/ExportFilter/
for examples of plugins, which are used with above code.

There a several other examples for plugins in this code, e.g. dynamically added functions to QComboBoxes.

magland
1st September 2007, 11:20
ChainLink is an example of such a program (chainlink.sourceforge.net).... users write arbitrary C++/Qt functions, compile them within the program, and then access those functions via Matlab-style syntax. Functions are loaded internally using QLibrary::load.

sincnarf
1st September 2007, 12:24
What on earth are you doing? What user base do you have? Did you think of the security implications of such a model?


I'm trying to create general image processing and image analysis framework that'll be used for teaching students in our university. It'll be used by the students (like me). The users are the ones who will create their own image processing functions. The qt project will be the one responsible for adding/deleting the users' functions. The whole project will be used for easier image manipulation, comparison, feature extraction etc. (hopefully)

Sorry but I haven't thought of security implications.

marcel
1st September 2007, 12:28
But you don't need to request source code from your users.
You should establish an interface for the user functions, have the users write their plugins while respecting that interface and compile them.

All your application has to do is load and use them.

You don't need anything as complicated as chainlinik or dynamic code replacement.
All you need is something as simple and common as a plugin system.
You can find a lot of examples, including in the Qt documentation.

For example, you could define the interface to consist in some resize functions, functions that provide image info, some raster manipulation like converting between color spaces, etc... The other students should provide these functions in their plugins(identified by name).

Regards

sincnarf
2nd September 2007, 15:27
ok. currently studying plugins

sincnarf
14th September 2007, 02:26
I would say: Forget it. Your user would have to write C++ within you application, compile it with an external compiler, link it during runtime to the application he is currently in?

What on earth are you doing? What user base do you have? Did you think of the security implications of such a model?

Nevertheless, I suppose you thought of all this. It can be done using QProcess to compile your stuff and using plugins to add the freshly compiled lib to your running application.

For an example of plugins:

http://spectrascan.svn.sourceforge.net/viewvc/spectrascan/SpectraScan/MainWidget/ExportPluginList.cpp?revision=52&view=markup
Method 'ExportPluginList::loadPlugins()'
and
http://spectrascan.svn.sourceforge.net/viewvc/spectrascan/SpectraScan/MainWidget/MenuBars/Actions/ExportFilter/
for examples of plugins, which are used with above code.

There a several other examples for plugins in this code, e.g. dynamically added functions to QComboBoxes.

I can't get to run spectrascan that I got from sourceforge. Hmmm. No progress still

wysota
14th September 2007, 09:02
To be honest QtScript seems perfect for you. It has a C/C++-like syntax, so students should be able to use it easily. If you want to stick with C++ nevertheless, you can always recompile and relink the application after adding the new function (Qt and qmake will make it trivial). That's what we do in our university during a similar course (without using Qt though).

magland
14th September 2007, 11:28
sincnarf,

How much of this app do you want to build yourself? If you want something that's ready out of the box to start with, I suggest (as I did before) chainlink. I can help you get started if you like - i.e. with installation, and creating first plugin functions. It may also give you some ideas for your app.

We use it in our MRI imaging process lab - and it was designed for this purpose.

It's a matlab console. So it could be used in this kind of way:

.................................................. .
Welcome to ChainLink Console
> X = read('myimage.jpg');
> Y = my_processing_function(X)
> viewimage(X); viewimge(Y);
.................................................. .

Here, "my_processing_function" is a C++ function that can be modified and recompiled from within user interface itself.

JM

sincnarf
18th September 2007, 15:56
To be honest QtScript seems perfect for you. It has a C/C++-like syntax, so students should be able to use it easily. If you want to stick with C++ nevertheless, you can always recompile and relink the application after adding the new function (Qt and qmake will make it trivial). That's what we do in our university during a similar course (without using Qt though).


guru wysota, I really would like things to be dynamic, after all, Qt can do what I am trying to implement. My question on plugins: what file format will my plugins be in the end? DLL? .a? .win? .cpp? .h? I don't know.



How much of this app do you want to build yourself?

any help will be greatly appreciated



I can help you get started if you like - i.e. with installation, and creating first plugin functions. It may also give you some ideas for your app.

what are the examples of the plugin functions? I need to teach the students how to create their own plugins too so I need to document it. I sort of need to create my own API for it.




.................................................. .
Welcome to ChainLink Console
> X = read('myimage.jpg');
> Y = my_processing_function(X)
> viewimage(X); viewimge(Y);
.................................................. .

I really would like to get everything going with the QGraphicsView Framework, even the application of the image processing function. But if there's no other way to use the functions, the second step, "> Y = my_processing_function(X)" is alright

sincnarf
18th September 2007, 16:04
But you don't need to request source code from your users.
You should establish an interface for the user functions, have the users write their plugins while respecting that interface and compile them.

All your application has to do is load and use them.


I sort of need a standard template for the creation of the plugins. Up to now, I have no idea.

wysota
18th September 2007, 16:28
My question on plugins: what file format will my plugins be in the end? DLL? .a? .win? .cpp? .h? I don't know.
Real plugins will be dynamic loaded objects (dll). But if you go for the script approach, they will be text files which you'll be able to reload on demand from within your application. An option is to use Python as well, but this requires students to be able to implement Python programs.

I suggest you start from thinking what features would you like the "added functionality" to have. Because if you're just after writing simple image processing functions, using javascript is the best way. When you use C++ plugins you still have to compile and reload them. Of course it's not that hard to do this, but still you have to implement it on your own. That's not a problem with using QtScript - you just load and run a script on demand (and they can be updated live). The only work you need is to create QAction objects for the menu based on available scripts.

sincnarf
19th September 2007, 09:31
I suggest you start from thinking what features would you like the "added functionality" to have.

I still don't know what functions to use. All I know is the users are the ones who will create such image processing functions (C++). Of course I know that I need to test the simple scripts, I'm thinking of simple things like binarization of an image or grayscaling or conversion to other image color modes.

With my current application, I need to create a template for the QtScripts that I will create. the template must be able to
1. accept an image from qt
2. use variables from my qt application.
Can qtScript do that?



void binarize(Image imageFromQt){
//manipulate pixels of imageFromQt using variables from my Qt Application
//pass the manipulated Image to a QIcon or QGraphicsView
}

void grayScale(Image imageFromQt){
//manipulate pixels of imageFromQt using variables from my Qt Application
//pass the manipulated Image to a QIcon or QGraphicsView
}


A final requirement is the students that will create the image processing functions must use C++ and only C++. Most examples of QtScript I see make use of JavaScript . . .

Also, can I use methods that are declared within my QtApplication to be used by the QtScript codes?

wysota
19th September 2007, 10:19
Well.. QtScript is javascript-like, so if the requirement is to use C++ then you have to go for real plugins. But first verify if that is a real requirement or just because someone said "hey let's make students implement their image processing functions in C++". If the goal is to implement an image processing function, then it really doesn't matter what language is used.

sincnarf
19th September 2007, 11:13
But first verify if that is a real requirement

Well that's what majority of the ComSci insctructors say. Use C++.

Is this correct? a qt plugin can be created with the use of a user-defined pure c++ file?

wysota
19th September 2007, 11:21
Well that's what majority of the ComSci insctructors say. Use C++.
So my natural question would be "why?".


Is this correct? a qt plugin can be created with the use of a user-defined pure c++ file?

Well... No :) But you can provide a template that uses Qt and students can fill that template using pure C++ (or whatever they like in fact, including QtScriptEngine).

magland
19th September 2007, 13:48
what are the examples of the plugin functions? I need to teach the students how to create their own plugins too so I need to document it. I sort of need to create my own API for it.


Here is an example of the ChainLink "rand" plugin function which returns a random multi-dimensional array of numbers.



//[function] rand
//input: integer [N1], integer [N2], integer [N3], integer [N4], integer [N5], integer [N6]
//output: mda ret
//description: define a multidimensional array of random numbers
bool rand(mda &ret, integer N1=-1, integer N2=-1, integer N3=-1, integer N4=-1, integer N5=-1,integer N6=-1);



... and the implementation ...



bool rand(mda &ret, integer N1, integer N2, integer N3, integer N4, integer N5,integer N6) {
ret.allocate(MDA_TYPE_REAL,N1,N2,N3,N4,N5,N6);
int32 j;
for (j=0; j<ret.size(); j++)
ret.set((real)rand()/RAND_MAX,j);
return true;
}


This is the kind of thing that the students would need to write to create custom functions. Here mda is a fundamental datatype in chaninlink (multi-dimensional array).

From the console it would just be

> X=rand(100,100);
> viewmda(X);




I really would like to get everything going with the QGraphicsView Framework, even the application of the image processing function. But if there's no other way to use the functions, the second step, "> Y = my_processing_function(X)" is alright

The QGraphicsView framework would certainly be possible even from within chainlink.

JM

sincnarf
19th September 2007, 14:08
So my natural question would be "why?".

C++ is already the standard P.L. for image processing in this university. Maybe because as compared to other programming languages that are taught here (Java, C, Matlab, etc ) C++ is the fastest on image sequences and the instructors already made their own libraries using C++. So as much as possible they would want to integrate their libraries to other future C++ programs created by us students.


But you can provide a template that uses Qt and students can fill that template using pure C++ (or whatever they like in fact, including QtScriptEngine)

Hmmm, how can I find examples of such templates that can be filled?

wysota
19th September 2007, 14:24
C++ is already the standard P.L. for image processing in this university. Maybe because as compared to other programming languages that are taught here (Java, C, Matlab, etc ) C++ is the fastest on image sequences and the instructors already made their own libraries using C++. So as much as possible they would want to integrate their libraries to other future C++ programs created by us students.

So why do you want to taint your pure C++ libraries with Qt code? If people were always thinking like that we'd be still writing our programs in binary code or assembly.

You won't be able to use Qt plugins in other non-Qt based applications in a straightforward way anyway. And if you'll want to incorporate Qt into them, you'll be able to incorporate the script engine as well. So the question still stands.

And the link you requested: http://doc.trolltech.com/latest/plugins-howto.html#the-lower-level-api-extending-qt-applications

sincnarf
19th September 2007, 15:12
So why do you want to taint your pure C++ libraries with Qt code? If people were always thinking like that we'd be still writing our programs in binary code or assembly.
I really am not sure, I'm not the one who selected the use of Qt anyway. Maybe because Qt is a fast way to build GUI (compared to others?), and Qt has a large base of users nowadays. So we want to be with the Qt community. Something like that.

wysota
19th September 2007, 15:53
I really am not sure, I'm not the one who selected the use of Qt anyway.

The only thing I'm saying is that maybe you should discuss (discuss, not ask) it with the person who introduced the C++ requirement. Maybe it's not important at all and you'll save yourself time and effort trying to implement automatic recompilation and reloading of C++ plugins into your application and you'll help the students using the application. I guess you could even use ChainLink as your base application and implement your image manipulating functionality there not having to write a single line of C++ code.

sincnarf
20th September 2007, 08:02
The only thing I'm saying is that maybe you should discuss (discuss, not ask) it with the person who introduced the C++ requirement. Maybe it's not important at all and you'll save yourself time and effort trying to implement automatic recompilation and reloading of C++ plugins into your application and you'll help the students using the application. I guess you could even use ChainLink as your base application and implement your image manipulating functionality there not having to write a single line of C++ code.
Well, just asked him a while ago. He's my adviser for my S.P. (Special Problem) - sort of like a thesis. He requires that I make the template for the students in pure C++ or mix of C++/Qt. End of story guru wysota..

Anyway I saw the Plug and Paint example in the Qt examples. The Filter Interface is the perfect example of what I want to implement... Although I don't know where pnp_extrafilters.dll came from. How can I make a template based on pnp_extrafilters? I want to create a modified pnp_extrafilters.dll that can select what widget it will apply its functions on. Will this be hard and does creating my own dll satisfy the requirement that i only use pure C++ or mix of C++/Qt? If not, then I have to make an extra effort in teaching other students how to create their own dlls that are not pure c++ / mix of c++/qt

wysota
20th September 2007, 11:50
Look at the link I gave you in #23.

BTW. I suggest you implement a QtScript-enabled plugin anyway, just to have an option ;) Especially that it's only a few lines of code...

sincnarf
20th September 2007, 19:41
Okay .. . error here


g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o "debug\SP.exe" object_script.SP.Debug -L"c:\Qt\4.3.1\lib" -L"c:\Qt\4.3.1\lib" -lmingw32 -lqtmaind -LC:/Qt/4.3.1/IADFUQ/SP/plugins -lpnp_extrafiltersd -lQtGuid4 -lQtCored4
./tmp\obj\debug_shared\main.o: In function `ZN5QListI7QStringE13node_destructEPNS1_4NodeES3_' :C:/Qt/4.3.1/IADFUQ/SP/../../include/QtCore/../../src/corelib/thread/qatomic.h:(.text$_ZN36Staticpnp_extrafiltersPlugin InstanceC1Ev[Staticpnp_extrafiltersPluginInstance::Staticpnp_ex trafiltersPluginInstance()]+0x9): undefined reference to `qt_plugin_instance_pnp_extrafilters()'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\SP.exe] Error 1
mingw32-make[1]: Leaving directory `C:/Qt/4.3.1/IADFUQ/SP'
mingw32-make: *** [debug-all] Error 2

wysota
20th September 2007, 21:09
Ok, but what is it?

sincnarf
21st September 2007, 13:55
Ok, but what is it?

error solved. still have the dilemma on the template that's supposed to be filled by my students (with pure c++ code)

sincnarf
21st September 2007, 13:56
Ok, but what is it?

error solved. still have the dilemma on the template that's supposed to be filled by my students (with pure c++ code)

Thanks for all the suggestions especially on chainlink i'm trying to study this still

sincnarf
22nd September 2007, 03:55
Look at the link I gave you in #23.

BTW. I suggest you implement a QtScript-enabled plugin anyway, just to have an option ;) Especially that it's only a few lines of code...

I think I really may have to use QtScript after all if I want to pursue Linux support for this system. Now studying QtScript. Thanks again for all comments and suggestions.