Friday, July 25th 2008, 2:43am UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Flubb

Beginner

Posts: 6

Location: Germany

Occupation: Student at the University of Cooperative Education

1

Friday, September 23rd 2005, 3:40pm

QT and dynamic link libraries

Hi guys,

I've a short little question about QT in dynamic link libraries (DLL's or shared objects).
Here a little history: Currently I am working on a open-source game which should be high plugable (support of many different modification by simple DLL plugin).

In the game's startup I open a DLL and start a function out of it. This function creates the login widget. After the user has entered his login information I'd like to unload the DLL and go ahead in the main-function and open another widget out of it.

Here are some pieces of code...



int main ( int argc, char **argv )
{
void *handle;
t_functions functions;
QApplication a (argc, argv);

MainWin = new MainWindow ();
functions.Set_Connect = &Set_Connect;

/* open shared object */
handle = dlopen ( "./liblogin.so", RTLD_NOW );
if ( handle == NULL )
{
printf ( "dlopen failed %s\n", dlerror () );
return 1;
}

/* load functions */
Init_t Init = (Init_t) dlsym ( handle, "Init" );
if ( Init == NULL)
{
printf ( "dlsym failed: %s\n", dlerror () );
dlclose ( handle );
return 1;
}

/* give the shared object all functions which are available */
Init ( &functions );

/* load another DLL function */
Login_Window_t Login_Window = (Login_Window_t) dlsym ( handle, "Login_Window" );
if ( Login_Window == NULL)
{
printf ( "dlsym failed: %s\n", dlerror () );
dlclose ( handle );
return 1;
}

/* create login window */
Login_Window ( MainWin );
MainWin->show ();

/* here we go */
a.connect( &a, SIGNAL( lastWindowClosed () ), &a, SLOT( exit () ) );
a.exec ();

// HERE WE GO!!!
/* close the library */
dlclose (handle);

Make_Other_Widget();

return 0;
}


I hope it's almost clear what I did in there and what I'd like to do. How can I tell QApplication to execute again. Because the function Make_Other_Widget() executes but I can't see any widget ;(

Has someone a little hint for me? Or even a solution?

Best Regards


Max
  • Go to the top of the page

jacek

Master

Posts: 2,729

Location: Warsaw, Poland

2

Friday, September 23rd 2005, 4:15pm

RE: QT and dynamic link libraries

Try this:

Source code

1
2
3
4
5
...
a.connect( &a, SIGNAL( lastWindowClosed () ), &a, SLOT( quit() ) );
a.exec ();
...
}
But IMO it would be better, if you redesign your application that it starts the main event loop only once.

Quoted

Has someone a little hint for me? Or even a solution?

QPluginLoader?
  • Go to the top of the page

Flubb

Beginner

Posts: 6

Location: Germany

Occupation: Student at the University of Cooperative Education

3

Friday, September 23rd 2005, 6:10pm

I looked around for some QPluginLoader stuff. I've found the QT tutorial, but this doesn't fit my needs, because I don't want to compile the plugin-headerfile into the program.
The plugin has to be as independent as possible from the main program.
Has someone an idea how I could handle that?

Regards


Max
  • Go to the top of the page

jacek

Master

Posts: 2,729

Location: Warsaw, Poland

4

Friday, September 23rd 2005, 6:19pm

Quoted

Originally posted by Flubb
The plugin has to be as independent as possible from the main program.
Has someone an idea how I could handle that?

Have ever heard about an interface or about an abstract base class?
  • Go to the top of the page

Flubb

Beginner

Posts: 6

Location: Germany

Occupation: Student at the University of Cooperative Education

5

Friday, September 23rd 2005, 7:06pm

Sorry, but I'm new to C++ and QT. I used to write C with GTK+ programs (easy procedual)
Could you give me a short and simple advice how to implement an interface or an abstract base class? Because the Trolltech tutorial is very poor on this section.
Thanks for your replies, because you might be my last hope.

Regards


Max
  • Go to the top of the page

Flubb

Beginner

Posts: 6

Location: Germany

Occupation: Student at the University of Cooperative Education

6

Friday, September 23rd 2005, 7:38pm

Hi,
I googled a lot around and finally I found out, that I can't use interfaces, because I'd like to use signals and slots also.
Now I've following big little question:
How could I redesign my source-code to support a twice loading of a.exec();?
Or running procedual code after closing a specific widget (outside of the library).
(By the way: a signal call will terminate with a segmentation fault )

Regards


Max
  • Go to the top of the page

jacek

Master

Posts: 2,729

Location: Warsaw, Poland

7

Friday, September 23rd 2005, 7:52pm

This is a simple, completely made up, example:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Abstract base class (ABC) that defines an login window interface
// this is what you use in your application
class LoginWindowInterface
{
   public:
      virtual bool authenticate() = 0;
      virtual QString login() const = 0;
      virtual QString password() const = 0;
};

// this class implements that interface, but it's provided by the plugin
// you don't need any headers in your application to use it
class LoginWindowImpl : public QDialog, public LoginWindowInterface
{
   public:
      LoginWindowImpl( QWidget *parent ) : QDialog( parent ) { _ui.setupUi( this ); }
     
      bool authenticate() { return (exec() == QDialog::Accepted); }
      QString login() const { return _ui.login->text(); }
      QString password() const { return _ui.password->text(); }
};

// abstract login window factory --- create login windows
// this is what your plugin returns when you invoke QPluginLoader::instance()
class LoginWindowFactory : public QObject
{
   public:
      LoginWindowFactory( QObject *parent ) : QObject( parent ){}
      virtual LoginWindowInterface * create( QWidget *parent ) = 0;
};

// concrete factory provided by the plugin
class LoginWindowFactoryImpl : public LoginWindowFactory
{
   public:
      LoginWindowInterface * create( QWidget *parent ) { return new LoginWindowImpl( parent ); }
};

// sample:

QObject *obj = pluginLoader->instance();
if( obj == 0 ) { ... }
LoginWindowFactory *factory = qobject_cast< LoginWindowFactory * >( obj );
if( factory == 0 ) { /* wrong plugin? */ }
LoginWindowInterface *loginWindow = factory->create( this );

if( loginWindow->authenticate() ) {
   QString login = loginWindow->login();
   QString password = loginWindow->password();
   ...
}
else {
  /* authentication was cancelled by the user */
}
You might want to read "C++ Primer" by Stanley B. Lippman and Josee Lajoie. There is also Bruce Eckel's book available online and here is a really good C++ FAQ.
  • Go to the top of the page

jacek

Master

Posts: 2,729

Location: Warsaw, Poland

8

Friday, September 23rd 2005, 7:55pm

Quoted

Originally posted by Flubb
I googled a lot around and finally I found out, that I can't use interfaces, because I'd like to use signals and slots also.

I don't see any problem in using signals and slots with plugins. Could you post a link to that site?

Quoted

Or running procedual code after closing a specific widget (outside of the library).

Does this widget happen to be a modal dialog?
  • Go to the top of the page

macroguy99

Beginner

Posts: 5

Location: Penang, Malaysia

9

Friday, May 16th 2008, 4:16am

How do I compile my program with dynamic load shared library using the Makefile created by qmake? Any parameters needed when I qmake my Makefile?
I'm so newwwwwwwwwwwww to Qt...
  • Go to the top of the page

Rate this thread