Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Function Pointer From String (static class member)

Function Pointer From String (static class member)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    Hi,

    What I am trying to do, is create a main() function with custom input arguments in a dll, and wrap it in a thread using QtConcurrent.

    I could do this easily if I just made a class, but I want to dynamically call the main function without having to create a class object to pass into QtConcurrent...

    So for example, I want a function in the hello.dll called main_ride_bike(BikerInfo *info), where the BikerInfo class contains a couple attributes about the biker, like how far he wants to go (integer), the bikers name (QString), etc.
    @
    // create function name
    QString processName = "main_ride_bike"; // function in dll

    // create biker info
    BikerInfo *info = new BikerInfo(this);
    info->bikerName = "mike";
    info->distanceToGo = 100;

    // start thread using 'main' function in dll
    QFuture <void> rideBike = QtConcurrent::run(processName, info);
    @

    How could I construct the Qt Dll to do so?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      in order to create correctly a shared library read "here":http://qt-project.org/doc/qt-5.1/qtdoc/sharedlibrary.html

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        I figured out most of what I was trying to do with the dll.

        My only issue now is trying to convert a string to a function pointer. Unfortunately, I don't believe I will be able to use a 'reflection' like technique in c++.

        Thank you again.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          The question is: "Why do you need use a string"?
          If you really needs it, Read about QLibrary class in order to load the library at runtime.

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on last edited by
            #5

            Hi,
            Converting a string to a function pointer is not possible directly, what is possible is to use "QMetaObject::invokeMethod()":http://qt-project.org/doc/qt-5.1/qtcore/qmetaobject.html#invokeMethod to call a method using its name in a string. But unfortunately this does not return a pointer to the function, so I'm not sure that this will match with you requirements.

            Maybe this post could help : "Call a function or methods dynamically":http://qt-project.org/forums/viewthread/17773

            edit: There is certainly a way to run QMetaObject::invokeMethod in a different thread.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved