Andy Shaw

Qt Commercial Support Weekly #7: Accessing custom datatypes with ActiveQt

12/12/2011 2:06 PM  | Posted by: Andy Shaw

Some people may think it is strange or surprising when I say that I am always learning in this job while I am supporting people.  That is one of the great things about Qt. There is always more to it as the versions pass by and I can learn more and more about how things work underneath.  One of these things recently was related to ActiveQt and custom interfaces.  For the benefit of those who don't know what ActiveQt is, it is primarily a wrapper around COM on Windows. It allows you to embed ActiveX controls in your application and also create ActiveX controls for use in other applications which aren't necessarily Qt based.  For a more detailed introduction you can find more information about it here.

 

So, as I mentioned before what I am going to talk about this week is custom interfaces in ActiveQt. What this provides is a means to interact with the COM control when you want to use a non-supported datatype by Qt.  What I mean by non-supported datatype is the ones that are not automatically converted from the native type to an existing Qt type.  When we want to handle this on the client side then we use queryInterface() on the object and get access to the interface that we want to use.  For example:

 

 

                IObjectSafety *objSafe = 0;

                axWidget->queryInterface(IID_IObjectSafety, (void **)&objSafe);

                objSafe->GetInterfaceSafetyOptions(…);

 

 

So, on the client side it is fairly straightforward. You get access to the interface you want to use and call the function directly on that interface.  So, what if you want to do it on the other side of things?  What if you want to provide a means to invoke functions on your control that is created with the help of ActiveQt?  For this we will have a struct called UserDefinedData. It doesn't matter what it is or what it provides - it is just so that we can use it as an example to demonstrate how to implement this.  So our struct looks like this:

 

 

                struct UserDefinedData

                {

                                int a;

                                int b;

                                int c;

                }

 

 

In order to be able to provide a means to access this or utilize this, we need to add a class that will provide access to it. For this case we will just add a means to get and to set the user defined data which will be a subclass of the IUnknown interface as this is a new interface not based on anything else.

 

 

                interface IUserDefinedClass : IUnknown

                {

                public:

                                HRESULT getUserDefinedData(UserDefinedData *data) = 0;

                                HRESULT setUserDefinedData(const UserDefinedData &data) = 0;

                };

 

 

In order to be consistent with what COM objects prefer for their functions, we have set HRESULT to be the return value of the functions.  What remains for us now is to make this available to our control that we are creating with the use of ActiveQt.  First of all we need to make sure we have an ID for this interface so that it is easy for the applications to request for this interface.  So to the header class that contains the UserDefinedClass definition we add the following:

 

 

                #include "Unknwn.h"

                #include <initguid.h>

 

                DEFINE_GUID(IID_IUserDefinedClass, 0x469849a5, 0x7272, 0x11d3, 0xb5, 0x15, 0xcc, 0x43, 0x83, 0x4e, 0x41, 0x67);

 

 

The GUID used should be unique, so use an UUID generator to get a unique one to use here.  The IID_ part should stay so as to be conformant, but the rest of the string does not need to be the same as the interface name, it just makes it easier to remember if you do this.  The next thing we need to do is ensure that this interface is used inside our ActiveX control.  To do this we need to use a class called QAxAggregated as this will enable us to provide our own queryInterface function which will enable users of the control to actually access the custom datatype.

 

 

                class UserDefinedClassAggregate : public QAxAggregated, public IUserDefinedClass   

                {

                public:

                                QAXAGG_IUNKNOWN

                                long queryInterface(const QUuid &;iid, void **iface);

                                HRESULT getUserDefinedData(UserDefinedData *data);

                                HRESULT setUserDefinedData(const UserDefinedData &data);

                };

 

 

I won't show the implementation of the user defined data functions here as that can basically be what you want it to be.  Firstly a word on the QAXAGG_IUNKNOWN macro, this is a convenience macro that provides an implementation of the QueryInterface(), AddRef() and Release() functions for you to save you from having to do so as IUserDefinedClass inherits from IUnknown.  The only function left then is the queryInterface() function which looks like

 

 

                long UserDefinedClassAggregate::queryInterface(const QUuid &iid, void **iface)

                {

                                *iface = 0;

                                if (iid == IID_IUserDefinedClass)

                                                *iface = (IUserDefinedClass*)this;

                                else

                                                return E_NOINTERFACE;

                                AddRef();

                                return S_OK;

                }

 

 

The code is fairly straightforward and speaks for itself which makes it easy to adapt for your own classes.  All that is left now is to make it available to your ActiveX control for usage.  This is done by reimplementing the createAggregate() function in your QAxBindable subclass.

 

 

                class MyTestControl : public QObject, public QAxBindable

                {

                                Q_OBJECT

                public:

                                MyTestControl(QObject *parent = 0);

                                QAxAggregated *createAggregate()

                                {

                                                return new UserDefinedClassAggregate;

                                }

                };

 

 

And that is all you need to do on the control side. I haven't given a complete implementation here, but you have the building blocks necessary to create your own control that gives access to custom datatypes.  To finish off here is how you would access this specific case in your other application.

 

 

                UserDefinedData myData;

                IUserDefinedClass *userDefinedClass = 0;

                obj.queryInterface(IID_ IUserDefinedClass, (void **)&userDefinedClass);

                if (userDefinedClass)

                                thirdPartyClass->getUserDefinedData(&myData);

 

 

As always, if you run into trouble then contact Qt Commercial Support via the Qt Commercial customer portal.

36 

Comments:

jh | 12/12/2011 9:04 PM
Great! Always good to see that desktop topics are discussed (even if they are not platform-independent). We are devloping commercial software that uses QActiveX and we have a strong interest in that part of Qt to be maintained in good quality. Good work!

jh | 12/12/2011 9:35 PM
Great! Always good to see that desktop topics are discussed (even if they are not platform-independent). We are devloping commercial software that uses QActiveX and we have a strong interest in that part of Qt to be maintained in good quality. Good work!

Andy Shaw | 12/13/2011 5:03 PM
jh: Thank you for the great comment and thank you for reading too :) My intention is to provide a mix of topics usually prompted by something that came up during the previous week in support as it shows what people are currently using and what the current issues potentially are. So sometimes there will be platform specific things as well as the platform independent topics. Hopefully that way there is something for everyone at one point :)

feedmeqt | 12/16/2011 5:48 AM
Hi, Thanks for this, Since we are also trying to achieve the same!!!

Bubby | 1/11/2012 1:40 PM
That insight would have saved us a lot of erffot early on.

zbxdyvs | 1/12/2012 11:56 AM
tDemIQ gnsglqtlrvtw

Indy | 1/25/2012 11:45 AM
This makes everything so completely palneiss.

cdxzrsc | 1/25/2012 7:25 PM
bc6WDX pwbfjiuiyhbf

shinyletters | 1/28/2012 5:11 AM
tugrul hocam hizina yetismek mumkun degil biz bi makaleyi hazmedemeden sen bir digerini yayinliyorsun. bu teknikle biz google mapse programimiz icinden takla bile attiririz valla tesekkurler… individual health insurance

KINGALBERTASH | 1/29/2012 12:23 PM
colchicine =DDD accutane purchase cqunn order accutane online 714

jezzyrulz | 1/31/2012 4:01 AM
buy valtrex cheap 870439 zovirax cream pharmacy online 68325

ShimonPeres | 2/5/2012 3:47 AM
Rica ederim üstadım, vazifemiz Google Maps konusunda haklısın, yakın bir zamanda onunla ilgili de makale yazmayı düşünüyordum. O yüzden JavaScript Delphi entegrasyonuna girmiştim Senden de bir şey kaçmıyor maaşallah insurance auto

Valthyst | 2/8/2012 3:19 PM
auto insurance quote fxqwa home insurance rates jmlm health insurance providers hoqgd

COBRARocky | 2/9/2012 12:30 PM
auto insurance quote 332414 individual health insurance plans 81569 business insurance quotes ycso

sakisai | 2/10/2012 6:15 AM
free auto insurance quotes tfupoh cheap health insurance 0459

Ziclali | 2/13/2012 6:12 AM
cheap health insurance ggq small business insurance :)))

KEKOLUL | 2/16/2012 6:19 AM
online auto insurance =PP life insurance for seniors 3784

xxxSexyRedxxx | 2/18/2012 4:27 AM
Here, here! Well said. An excellent post!Authors who already have a foot in the publishing door need this reminder, too. The struggle between literary and commercial is never more apparent than today when the economy is a factor and authors are desperate for book sales. Years ago, one of my writing mentors posed a similar pick your poison' question. What gods will you bow down to? she asked. Now, I can answer: Why bow at all? I choose to sit up straight at my desk each morning and write my stories. Plain and true. I choose to write the best darn way I know how, and if I keel over mid-sentence, well, so be it. The poisons win. But in the sheer act of genuine creation, I believe authors can find natural immunity.It's wonderful to hear literary professionals championing the virtues of simple, good literature. No labels attached.Yours truly, Sarah buy intravenous tramadol classic car insurance

Supachrissy | 2/20/2012 4:55 AM
When I translate to Japanese, Did you forget the ending . ? is appeared,but in Japanese, end punctuation is ? .Can you fix or add disable option for Japanese? tramadol online lowest price generic cialis online

Maria | 2/21/2012 9:48 PM
| July 8, 2009 HiAwesome puglin, I got it to work in regards to shadowbox the video and get it to play however in my wordpress post I just see the text Click here to watch but I don't see a image thumbnail at all. I have even tried to upload a custom one.Is there something i'm missing?-Tobias

eqshszedqx | 2/23/2012 12:13 PM
jDWcKg podrrahtvnhq

AlexMeadus | 2/24/2012 4:31 AM
Bravo les filles pour cette energie Corentin est un copain de mon fils Thomas, ce garcon a une peche d'enfer Je vous dis felicitations A bientotBisous car insurance quotes viagra

Bloxkcatel | 3/6/2012 11:16 AM
United StateI have been interested in this topic for quite some time. I have been researching it for a couple of hours and found your post to be very interesting. Cheers. Personal Injury Attorney Las Vegas

Jase | 3/8/2012 3:59 AM
Paki, aunque con retraso, te comunico que todos los que lo solicist teis a trav s de los coemntarios est is admitidos/as ultram er car insurance rates

Tish | 3/14/2012 4:54 AM
Paki, aunque con retraso, te comunico que todos los que lo solicist teis a trav s de los coemntarios est is admitidos/as car insurance in florida auto insurance quotes

Kaden | 3/23/2012 7:33 PM
car insurance qoutes 8-[[[ health insurance quotes 075009

eleen | 3/26/2012 9:27 AM
)Write the program from the scratch on each OS , i mean writing the program on Windows using Viusal Studio , on Mac use Xcode . but this costs alot ... i own Windows PC , buying Mac or Mac OS for my desktop, will ruin my budget . somanabolic muscle maximizer review

joseph | 3/26/2012 9:35 AM
How to make Qt Widgets resize automatically? Two identical identical program built in Visual Basic and another in Visual C++ have this ability, but I want my Qt program to do this because it wis eventually going to deploy on Linux. Any help is appreciated. burn the fat feed the muscle review

star09 | 4/9/2012 1:07 PM
There are a lot of blogs and articles out there on this topic, but you have captured another side of the subject. Regards, Essay Writing Help


Toolman11 | 4/13/2012 12:19 PM
You cant admit this "Tyler Clementi’s suicide is just one of the many recent deaths due to anti-gay abuse. In Tehachapi, " Regards, Discount Power Tools

Toolman11 | 4/13/2012 1:02 PM
You need to admit this "There are an estimated 27 million individuals worldwide who are currently victims of human trafficking, according to Kevin Bales, an expert on human trafficking. " Regards, Replacement Parts

ethanlord | 4/14/2012 3:05 PM
Recently I have found your site and wanted to say that I have really enjoyed browsing your posts. qualified dissertation writers , dissertation ideas , quality dissertation service , qualified dissertation writers , professional dissertation provider , plagiarism free dissertation proposal.

adamsavyer | 4/14/2012 3:08 PM
I just didn't know. I am glad to see that people are actually writing about this issue in such a smart way, showing us all different sides to it. You are a great blogger. Please keep it up. professional seo services Birmingham I cant wait to read whats next..


Coach Clearance | 5/5/2012 7:53 AM
Welcome to buy

Cheap Coach Purses

for sale online if you want to get the best price for your dream coach on

Coach Purses Outlet

.

Coach Online Outlet

have various types for your choices in

Coach Purses Outlet

, you'll find selections to meet any budget from

Coach Outlet Online

. Here are some detailed of discount

Coach Online Sale

.Elegant and noble will be the distinctive characteristic of authentic
User verification Image for user verification  
     

Tags

Archive

Authors

Pasi Matilainen

Pasi is a Software Specialist working at Digia, Qt Commercial R&D and he concentrates on Mac OS X development. Pasi holds an M.Sc. degree in Information Technology from the Tampere University of Technology, Finland.

Tarja Sundqvist

Tarja is a Senior Software Engineer in the Digia, Qt Commercial Support team. She has been working in Digia for over 10 years in various positions: software development, testing, error management. Now, Tarja is focusing on helping Qt Commercial customers with their daily Qt problems on Windows and Linux platforms. Tarja holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Akseli Salovaara

Akseli is a Software Specialist at Digia, Qt Commercial R&D and is responsible for the Qt Commercial releases and deliveries. Akseli holds an B.Sc. degree in Information Technology from the University of Applied Sciences in Jyväskylä, Finland.

Samuli Piippo

Samuli is a Software Specialist at Digia, Qt Commercial R&D with a concentration on  embedded Linux and RTOS development. Samuli holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Katherine Barrios

Katherine is the Marketing Manager at Digia, Qt Commercial. She is responsible for getting the word out about Qt Commercial to the Qt ecosystem and working together with our customers and the Qt community to further extend the love for Qt on desktop and embedded. She was previously employed at Nokia, Qt Development Frameworks as Program Marketing Manager and is based in Oslo, Norway.

Sami Makkonen

Sami is a Senior Product Manager working at Digia, Qt Commercial R&D and he is responsible for the product planning including new feature development and enhancements to existing functionality. Sami holds an M.Sc.(Econ.) degree in Computer Science.

Andy Shaw

Andy is the Head of Support at Digia, Qt Commercial and has been working with Qt and supporting customers using Qt for 11 years.  He thrives on solving customer problems and getting feedback from them.

Tuukka Turunen

Tuukka is the Director of R&D at Digia, Qt Commercial and is responsible for the planning, creation, verification and delivery of the Qt Commercial product. Tuukka holds a M.Sc.(Eng) and Licentiate of Technology degrees in Computer Science.

Qt Commercial Team