tags:

views:

524

answers:

5

Is there any way to open a Qt process and then programatically add widgets? For example, start a process that opens a frame and then, with another call from, say, another program (that is mine; actually another scripting language), add a button to that frame?

So, in another program, I say "open frame" at the prompt and the frame opens. And then I say "add button" and the frame just opened is populated with a button.

Updated to address comment. Updated to add example.

A: 

That functionality is not built-in to Qt applications (or any window other fraemwork AFAIK), but you can build it in yourself if you are creating both programs.

Bryan Oakley
Good to hear that it's possible--thanks!
Jeff
A: 

The best way to start doing this would using QSharedMemory to access the same memory in both processes and then what you describe should be perfectly possible.

Mike McQuaid
I see an example @ http://doc.trolltech.com/main-snapshot/ipc-sharedmemory.html, and this makes sense for memory-loadable things, but how about calls? So, in another language, I make I say "open frame" in my scripting language and the frame opens. And then I say "add button" and the frame just opened is populated with a button. This seems like the right track though....
Jeff
And thanks for the reply--congrats on the recent engagement.
Jeff
You just need to cast the correct address to a pointer. In this case you might cast an address to the QFrame and then you can call functions on it. You'll need to be careful with allocation to ensure that you allocate into the shared memory.
Mike McQuaid
And thanks about the engagement :)
Mike McQuaid
A: 

If you can make the other app a dll/shared library that is loaded by the main app.
Then simply have a method draw menu or draw frame in the dll. The main app checks if the dll is present and loads it, it can then check if the gui method exists - this is the common way to implement gui plugins.

Martin Beckett
A: 

The easiest way would be to expose a set of functions from the main program (the one that draws) and load them from the "client" and simply call them when you need to.

Another approach that requires more work would be to use some sort of inter-process communication (such as shared memory, sockets, d-bus etc. for implementations you can either use the ones that Qt offers or boost):

A simplified example using sockets would be to have the main program listen on a socket waiting for commands and invoking the right function for each command. While the other program communicating with it upon receiving commands from the prompt.

Idan K
After IPC is set up, look at QUILoader (http://doc.qtsoftware.com/4.5/quiloader.html). It can load .ui files on the fly, making it easy to add new UI to running program.
Eugene
+1  A: 

As already stated, it is possible. If you need a domain specific scripting language, I'd recommend using the QtScript module. It's based on ECMAScript 262 and allows access to signals, slots, and other things.

Although you could use IPC or TCP for controlling the application, Qxt provides QxtRPCPeer that can be used to connect signals and slots between applications connected via a QIODevice, such as a TCP or local connection.

Kaleb Pederson
+1 for QxtRPCPeer, which is really nifty.
Marcus Lindblom