Results 1 to 11 of 11

Thread: QLibrary

  1. #1
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QLibrary

    I want to load the library dynamically depending upon the users choice. How do I do it? QLibrary does not seem to just load the library when I do not put the library in LIBS += .... If I check lib.isLoaded(), it gives me true but I cannot use the library.
    I have now idea how does this QLibrary works? Please help me out.
    I am simply using QLibrary lib("libraryName"); lib.load().

    Thank you very much in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QLibrary

    I have now idea how does this QLibrary works?
    How about reading the docs to start with?
    QLibrary
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLibrary

    offcourse I started with reading the documentation first. But I could not understand most of the things like resolve() method. I did not fully understand the example given in the documentation .

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QLibrary

    Well "not understand" is a very broad term, I can only repeat what is in the documentation, which is very good in my opinion, and if you can't understand the documentation, you probably wont understand anything I will write either (as the quality of the documentation is probably better than anything any of us here can write on a forum post).
    Try to be more specific about what it is you don't understand, maybe we can help you then.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLibrary

    Ok! First of all my problem:
    I have two mount drivers - which is basically bundled up as dll files. Both of the libraries has same interface class. If I call the interface class, it will load the driver classes depending upon which library I load.
    It works perfectly fine when I change .pro file LIBS += ... , before compiling the program.
    Now I want to load the library dynamically, basically I want to give an option to the user which library (driver) he/she wants to use and load the driver accordingly.

    For this, I thought QLibrary is the thing. I am even not sure if QLibrary is the right thing for my purpose. Anyways, I called QLibrary mylib("libraryName") and mylib.load(). Doing this the library is loaded (which is confirmed by mylib.isLoaded()). However, my program gives error.

    In the documentation, resolve(QString symbol) method is mentioned. I did not understand what exactly is this "symbol". Is this the name of the class that I want to use from the library? If so how do I use it? I tried giving the name of the class as input variable to this method but it did not work. I am sorry, if I am being completely stupid here.

    Thanks and looking forward to your suggestions.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QLibrary

    Well, a library exports functions, and each exported function is a symbol (plainly put).
    So if you want to call function in the lib, you need the address of that function code, which you get by resolving the exported function symbol.
    So if you look at the example (see comments):
    Qt Code:
    1. QLibrary myLib("mylib");
    2. typedef void (*MyPrototype)(); //<---- this is a function pointer type for the function you need
    3. MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol"); //Here the function pointer gets the address from the lib
    4. if (myFunction)
    5. myFunction(); //here, if the pointer is valid, you can call the function in points to.
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLibrary

    Hi,
    Thank you for the post. I seem to understand better now. But still I am not able to make it work. I think the real problem is exporting the symbol from the library. Actually this library is compiled in C++ so as per the documentation, I have to export it as extern "C". I am doing something wron I know.
    let me explain my problem.
    In my library I have a class called I_MountInterface. Now, I want to use this class in my main program using QLibrary. I am still not sure about how to call "symbol" in resolve method.
    I did it in the following way:
    Qt Code:
    1. I_MountInterface *mount;
    2. typedef I_MountInterface* (*MyPrototypeOne)();
    3. MyPrototypeOne myFunction = (MyPrototypeOne) lib.resolve(" new I_MountInterface");
    4. if (myFunction){
    5. mount = myFunction();
    6. }
    7. else std::cerr << "myFunction is not valid" << std::endl;
    To copy to clipboard, switch view to plain text mode 

    And I am not sure how to export I_MountInterface class as extern "C" in my library. For testing i did it like following in the constructor of I_MountInterface class in my library:
    Qt Code:
    1. extern "C" MY_EXPORT I_MountInterface::I_MountInterface() {
    2. std::cout << "I am number 2" << std::endl;
    3. mountDriver = new I_MountDriver;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Thank you

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QLibrary

    This is not a Qt issue, but a general C++.
    Google about exporting classes in DLLs.
    In Qt, if you use Qt creator or the VisualStudio add in, you can select a project type which is a Qt library.
    It will automatically add the correct export/import defines to your class declarations - but you should really first read about it and understand the general concept and how it works.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLibrary

    I am using Eclipse. Anyways, I will do some more research about it. Thanks

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

    Default Re: QLibrary

    The general approach with resolving classes from C libraries is to have a base class definition that is linked to both the main application and the library and to have a subclass of that class defined in the library along with a factory method that instantiates object of the subclass and casts them to the base class. The approach is identical to using plugins. If all of the code is yours to design and implement, I suggest you indeed use the plugin approach (using QPluginLoader). Otherwise you'll have to resolve all the symbols you need from the library using QLibrary which can be pain in the... neck.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLibrary

    Thanks a lot for the idea of QPluginLoader. It took me some time to lean how to create plugin and use it. But at the end, it works like a charm.
    Thanks again
    Amita

Similar Threads

  1. How to use QLibrary?
    By digog in forum Newbie
    Replies: 22
    Last Post: 5th November 2010, 18:01
  2. QLibrary resolving functions
    By td in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2010, 12:56
  3. QLibrary in PyQt
    By Urthas in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 18:46
  4. The problem with QLibrary, help me!
    By dungsivn in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2008, 14:03
  5. Qlibrary
    By rianquinn in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2006, 12:23

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.