Using QLibrary to load a QWidget



  • Hi all,
    I created a library (under Linux) with Qt Creator containing a QWidget sublass: MyClass.

    Now I'm trying to load it with QLibrary from another Qt application.

    I don't understand how to proceed after this:
    @
    QLibrary library("../libs/libmyclass");
    @

    The documentation show an example:
    @
    QLibrary myLib("mylib");
    typedef void (*MyPrototype)();
    MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
    if (myFunction)
    myFunction();
    @
    but this isn't my situation because I need to get a class (QWidget subclass) from the library not a function.



  • Just include your MyClass.h file, and after that:

    @QLibrary library("../libs/libmyclass");
    library.load();
    if(library.isLoaded()){
    MyClass * c = new MyClass;
    if(c){
    // dostuff
    }
    }@



  • [quote author="HuXiKa" date="1306873460"]Just include your MyClass.h file, and after that:

    @QLibrary library("../libs/libmyclass");
    library.load();
    if(library.isLoaded()){
    MyClass * c = new MyClass;
    if(c){
    // dostuff
    }
    }@[/quote]

    I thought that using QLibrary there was a way to avoid the need to include .h file .
    Isn't that?



  • You can't use libs without a header file (at least it's not easy). Not just in Qt, but in general.
    If you really need something and don't have the header file, use "Dependency Walker":http://www.dependencywalker.com/ and create your own header with help from the program.
    But since you wrote the lib yourself, why is it a problem to include that .h file?



  • Thanks,
    I'll try to use plugins...



  • If you use late binding with dlls (which you do using QLibary::load), you have to use C exported functions, not classes. If you want to use classes (not their interface!) from a dll, you have to link against the dll.

    Only option around: using interfaces, which means, create a C exported function as creator function which returns a pointer to an interface (pure virtual class). The disadvantage of this is, you can't create those objects on the heap.



  • [quote author="Gerolf" date="1306910864"]If you use late binding with dlls (which you do using QLibary::load), you have to use C exported functions, not classes. If you want to use classes (not their interface!) from a dll, you have to link against the dll.

    Only option around: using interfaces, which means, create a C exported function as creator function which returns a pointer to an interface (pure virtual class). The disadvantage of this is, you can't create those objects on the heap.[/quote]

    Thanks,
    this is why documentation only show this:
    @
    QLibrary myLib("mylib");
    typedef void (*MyPrototype)();
    MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
    if (myFunction)
    myFunction();
    @

    The solution to create a "creator function" also require the header include... right?
    So it probably should be better to use a plugin.



  • And what do you need to use the plug-in? :-)
    the creator function approach uses the interface technique, which means, you have a header for the interface, but not for the specific implementation.

    If you use plug-ins (Qt-plugins) and it is enough for you to have the QWidget interface for the class (and the rest only via signal/slot or Q_INVOKEABLE methods) you can use plug-ins. otherwise you also need header files.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.