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. Using QLibrary to load a QWidget
Forum Updated to NodeBB v4.3 + New Features

Using QLibrary to load a QWidget

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 8.2k 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HuXiKa
      wrote on last edited by
      #2

      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
      }
      }@

      If you can find faults of spelling in the text above, you can keep them.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        [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?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HuXiKa
          wrote on last edited by
          #4

          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?

          If you can find faults of spelling in the text above, you can keep them.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              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.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  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.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  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