PDA

View Full Version : Qt interface based programming



thedoctor
26th June 2009, 10:41
Hi,

I am new to Qt and developing a product where I would like to use interface based programming. Essentially, this means mimicking Microsoft COM (i.e. all interfaces are derived from IUnknown).

I am not really 100% familiar with the Qt object model so I am not sure I can do this. With com usually the first interface implemented is IUnknown, but I notice that with Qt I have to subclass from QObject first and then I can list the other classes/interfaces I am implementing.

Anyone have any experiences or suggestions on how to proceed?

Regards.

AcerExtensa
26th June 2009, 11:02
http://doc.trolltech.com/4.5/activeqt.html

thedoctor
26th June 2009, 12:34
Hi,

ActiveQt is only supported on Windows. I am trying to mimick the functionality in a portable way. For this really I just need to implement IUnknown and follow com programming principles. I don't need the com infrastructure as I have only a couple of DLL's and I am writing a special function that uses DllGetClassObject to fetch the appropriate interface.

The problem I am coming across is that if I implement IUnknown as the first interface I get compile errors. I can only fix this by putting QObject as the first class in the inheritance hierarchy.

e.g.

class CMyComponent : public IMyComponent, public QObject {
Q_OBJECT
...

gives compile errors

BUT,

class CMyComponent : public QObject, public IMyComponent {
Q_OBJECT

is OK. But as far as I know, in com world this is not the way to do it.

Regards.

shentian
26th June 2009, 18:08
The Qt plugin system (http://doc.trolltech.com/4.5/plugins-howto.html) allows you to define interfaces using Q_DECLARE_INTERFACE and later extract interfaces from a QObject using qobject_cast. Here (http://doc.trolltech.com/4.5/tools-plugandpaint.html) is a good example. You can leave the plugin loading stuff away and link everything statically.

Maybe this is what you are looking for?

thedoctor
26th June 2009, 20:13
Hi,

Thanks for that suggesstion. I did look into using plugins instead but gave up the idea because you can only have 1 plugin per dll, I didn't want to statically link them to the main application as I want to be able to update the interfaces without updating the main app (via a software update service).

I wonder if the following would work:-

1. Declare all interfaces as plugins in a single dll.
2. statically import them in the same dll, and then use dllgetclassobject with qobject_cast to get the interfaces.

Regards.