Include Library into Library. Load Library at runntime. ( some Library Magic :) )



  • Hey guys, i need some help at working with librarys.

    this is my structure:

    • MyClass (subclass of QWidget)
      this class sould implement some functions, which should be used by the subclasses.
      this class is packed into a library

    • MySubClass (subclass of MyClass)

    • MySubClass1 (subclass of MyClass)

    • ... many more subclasses of MyClass
      all these classes where packed into different librarys
      theses classes have different UIs wich sould be loaded into a different programm

    now i want to load the different MySubClassX librarys at runntime and show their UIs. I hope you get what i want to do? Here is my code:

    MyClass.pro

    @QT += widgets

    TARGET = MyClass
    TEMPLATE = lib

    DEFINES += MYCLASS_LIBRARY

    SOURCES += myclass.cpp

    HEADERS += myclass.h
    myclass_global.h

    unix:!symbian {
    maemo5 {
    target.path = /opt/usr/lib
    } else {
    target.path = /usr/lib
    }
    INSTALLS += target
    }@

    MyClass.h

    @#ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QtCore>
    #include <QWidget>
    #include "myclass_global.h"

    class MYCLASSSHARED_EXPORT MyClass : public QWidget
    {
    Q_OBJECT

    public:
    MyClass(QWidget *parent = 0);

    protected:
    QString someString;

    public slots:
    int doSomething( int a, int b );

    };

    #endif // MYCLASS_H
    @

    MyClass_global.h

    @#ifndef MYCLASS_GLOBAL_H
    #define MYCLASS_GLOBAL_H

    #include <QtCore/qglobal.h>

    #if defined(MYCLASS_LIBRARY)

    define MYCLASSSHARED_EXPORT Q_DECL_EXPORT

    #else

    define MYCLASSSHARED_EXPORT Q_DECL_IMPORT

    #endif

    #endif // MYCLASS_GLOBAL_H
    @

    MyClass.cpp

    @#include "myclass.h"

    MyClass::MyClass(QWidget *parent) :
    QWidget(parent)
    {
    }

    int MyClass::doSomething(int a, int b)
    {
    return a+b;
    }
    @

    MySubClass.pro

    @QT += widgets

    TARGET = MySubClass
    TEMPLATE = lib

    INCLUDEPATH += "C:/Testing/" #here are the .h -Files of MyClass
    LIBS += "C:/Testing/MyClass.dll"

    DEFINES += MYSUBCLASS_LIBRARY

    SOURCES += mysubclass.cpp

    HEADERS += mysubclass.h
    mysubclass_global.h

    unix:!symbian {
    maemo5 {
    target.path = /opt/usr/lib
    } else {
    target.path = /usr/lib
    }
    INSTALLS += target
    }

    FORMS +=
    mysubclass.ui@

    MySubClass.h

    @#ifndef MYSUBCLASS_H
    #define MYSUBCLASS_H

    #include <QtCore>
    #include <QMainWindow>
    #include "mysubclass_global.h"
    #include "myclass.h"

    namespace Ui {
    class MySubClass;
    }

    class MYSUBCLASSSHARED_EXPORT MySubClass : public MyClass
    {
    Q_OBJECT

    public:
    MySubClass(MyClass *parent = 0);
    ~MySubClass();

    private:
    Ui::MySubClass *ui;

    };

    extern "C" MYSUBCLASSSHARED_EXPORT MySubClass* create();

    #endif // MYSUBCLASS_H
    @

    MySubClass_global.h

    @#ifndef MYSUBCLASS_GLOBAL_H
    #define MYSUBCLASS_GLOBAL_H

    #include <QtCore/qglobal.h>

    #if defined(MYSUBCLASS_LIBRARY)

    define MYSUBCLASSSHARED_EXPORT Q_DECL_EXPORT

    #else

    define MYSUBCLASSSHARED_EXPORT Q_DECL_IMPORT

    #endif

    #endif // MYSUBCLASS_GLOBAL_H
    @

    MySubClass.cpp

    @#include "mysubclass.h"
    #include "ui_mysubclass.h"

    MySubClass::MySubClass(MyClass *parent) :
    MyClass( parent ),
    ui( new Ui::MySubClass )
    {
    ui->setupUi(this);
    }

    MySubClass::~MySubClass()
    {
    delete ui;
    }

    extern "C" MySubClass* create()
    {
    MySubClass newinstance;
    return &newinstance;
    }

    @

    LoadLibrary.pro

    @QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = LoadLibary
    TEMPLATE = app

    INCLUDEPATH += "C:/Testing/"

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui
    @

    LoadLibrary.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QLibrary>
    #include "myclass.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow ui;
    //MyClass
    instance;
    };

    #endif // MAINWINDOW_H
    @

    LoadLibrary.cpp

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    typedef MyClass (*MySubClass)();
    QLibrary myLib("MySubClass");
    MySubClass create = (MySubClass) myLib.resolve("create");
    if(create){
    MyClass a = create();
    a->show();
    }
    }

    @

    main.cpp

    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    so until now it dosn't work. in LoadLibrary.cpp is an error. I can't get an instance of my MySubClass class.
    what am i doing wrong? All the .h-files and .dll are in the directory "C:/Testing/".


  • Lifetime Qt Champion

    Hi,

    Did you check that the library is loaded ? What errorString returns ?



  • @
    extern "C" MySubClass* create()
    {
    MySubClass newinstance;
    return &newinstance;
    }
    @

    Hi,

    You return a wild pointer here.


Log in to reply
 

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