Page 1 of 2 12 LastLast
Results 1 to 20 of 32

Thread: Dynamically add functions to qt program

  1. #1
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Dynamically add functions to qt program

    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.
    Image Analysis Development Framework Using Qt (IADFUQ)

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    You mean something like some sort of application plugins?

  3. #3
    Join Date
    Aug 2007
    Location
    Russia
    Posts
    19
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    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...

  4. #4
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    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?

    binary.cpp

    binary.h
    Last edited by sincnarf; 1st September 2007 at 03:43. Reason: attached code
    Image Analysis Development Framework Using Qt (IADFUQ)

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    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

  6. The following user says thank you to marcel for this useful post:

    sincnarf (15th September 2007)

  7. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Dynamically add functions to qt program

    Quote Originally Posted by sincnarf View Post
    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.n...52&view=markup
    Method 'ExportPluginList::loadPlugins()'
    and
    http://spectrascan.svn.sourceforge.n.../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.

  8. The following user says thank you to Kumosan for this useful post:

    sincnarf (15th September 2007)

  9. #7
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically add functions to qt program

    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.

  10. The following user says thank you to magland for this useful post:

    sincnarf (15th September 2007)

  11. #8
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    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.
    Image Analysis Development Framework Using Qt (IADFUQ)

  12. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    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
    Last edited by marcel; 1st September 2007 at 12:34.

  13. The following user says thank you to marcel for this useful post:

    sincnarf (15th September 2007)

  14. #10
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    ok. currently studying plugins
    Image Analysis Development Framework Using Qt (IADFUQ)

  15. #11
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    Quote Originally Posted by Kumosan View Post
    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.n...52&view=markup
    Method 'ExportPluginList::loadPlugins()'
    and
    http://spectrascan.svn.sourceforge.n.../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
    Image Analysis Development Framework Using Qt (IADFUQ)

  16. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically add functions to qt program

    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).

  17. The following user says thank you to wysota for this useful post:

    sincnarf (15th September 2007)

  18. #13
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically add functions to qt program

    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

  19. The following user says thank you to magland for this useful post:

    sincnarf (15th September 2007)

  20. #14
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    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
    Last edited by sincnarf; 18th September 2007 at 16:00. Reason: insufficient reply
    Image Analysis Development Framework Using Qt (IADFUQ)

  21. #15
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    Quote Originally Posted by marcel View Post
    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.
    Image Analysis Development Framework Using Qt (IADFUQ)

  22. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically add functions to qt program

    Quote Originally Posted by sincnarf View Post
    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.

  23. The following user says thank you to wysota for this useful post:

    sincnarf (19th September 2007)

  24. #17
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    Quote Originally Posted by wysota View Post
    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?

    Qt Code:
    1. void binarize(Image imageFromQt){
    2. //manipulate pixels of imageFromQt using variables from my Qt Application
    3. //pass the manipulated Image to a QIcon or QGraphicsView
    4. }
    5.  
    6. void grayScale(Image imageFromQt){
    7. //manipulate pixels of imageFromQt using variables from my Qt Application
    8. //pass the manipulated Image to a QIcon or QGraphicsView
    9. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by sincnarf; 19th September 2007 at 10:24. Reason: avoid double post
    Image Analysis Development Framework Using Qt (IADFUQ)

  25. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically add functions to qt program

    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.

  26. The following user says thank you to wysota for this useful post:

    sincnarf (19th September 2007)

  27. #19
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically add functions to qt program

    Quote Originally Posted by wysota View Post
    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?
    Image Analysis Development Framework Using Qt (IADFUQ)

  28. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically add functions to qt program

    Quote Originally Posted by sincnarf View Post
    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).

Similar Threads

  1. Qt/windows: how to compile program with audiere library
    By punjabikura in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2009, 15:09
  2. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.